📄 cplusplus.rs.html
字号:
<html><!-- Mirrored from c-faq.com/misc/cplusplus.rs.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:02:55 GMT --><head><title></title></head><body>From: Richard Stamp<br>Newsgroups: comp.lang.c<br>Subject: Re: FAQ 20.27: Is C++ a superset of C? Can I use a C++ compiler to compile C code?<br>Date: Mon, 1 Feb 1999 10:24:40 -0000<br>Message-ID: <793v82$hs6$1@flex.london.pipex.net><p>Larry Weiss wrote...<br>>What specific instances can anyone recall of C code having<br>>different semantics if given to a C++ compiler? (I don't have<br>>the H&S reference cited above, but I assume that it details<br>>some of these scenarios?)<br><p>I posted the following on 14 Feb 1998 summarising the cases noted in H&S.Apologies for the somewhat assertive phrasing -- it was appropriate incontext:<p>---<br>In C++, you can't use the identifiers asm, bool, catch, class, const_cast,delete, dynamic_cast, false, friend, inline, mutable, namespace, new,operator, private, protected, public, reinterpret_cast, static_cast,template, this, throw, true, try, typeid, using, or virtual. You can'tdeclare a type inside a struct definition and have it visible outside.You can't have storage class specifiers in type declarations. Youcan't refer to a top-level 'const' object with no explicit storageclass from another translation unit. You can't initialise a characterarray from a string literal which is "too long". You can't call afunction without declaring it first. You can't use old-style functiondefinitions. You can't use enumeration constants as integers (withoutan explicit cast). You can't convert from a '<TT>void *</TT>' to another pointertype without an explicit cast. You can't declare types inside sizeofexpressions, parameter lists or return types. You can't jump into acompound statement if this would skip declarations with initialisers.You can't do a 'return' with no value from a non-void function, evenif you never use the "result". You can't call 'main' recursively.<p>Also, you may get different behaviour in C++ because: // comments arerecognised; structure tags are implicitly declared as typedef names;a declaration like 'int f()' is a prototype declaration and signifiesthat the function takes no arguments.<br>---<p>If by "different semantics" you mean a program which is valid C and validC++ but has a different meaning under each, the examples of this tend to bea bit improbable. The one which always surfaces on occasions such as theseis<p><pre> int x = 2 //* Would anyone actually write this? */ 2 ;</pre><p>which sets x to 2 in C++ (because everything after the // disappears) or 1in C (because only the /*...*/ goes).<p>Another one is:<p><pre> typedef int x; { struct x { int a, b; }; printf ("%d\n", (int) sizeof (x)); }</pre><p>This prints sizeof(int) in C. In C++, the struct tag 'x' shadows the outertypedef so the size of the structure -- at least 2*sizeof(int) -- isdisplayed.<p>Cheers,<br>Richard</body><!-- Mirrored from c-faq.com/misc/cplusplus.rs.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:02:55 GMT --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -