Writing correct Cplusplus

From Apache OpenOffice Wiki
Revision as of 09:52, 5 January 2006 by Sb (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

C++ is an extremely complex language, and it is easy to accidentially write code that does not conform to The Standard. What is worse, there are cases where some compilers will not complain about your conformance violations, so that broken source code creeps into our code base. Only when someone later uses a pickier compiler will the error be noticed. (That is what just happened when trying to compile our code base with GCC 4.1, which is pickier than any of the compilers routinely used on the standard platforms until now.)

The following is a list of some issues you should have in mind when writing C++ code:

Extra Qualification

When declaring a member m of a class C, do not qualify m as C::m. That is, write [cpp] class C { void m(); }; and not [cpp] class C { void C::m(); };

Friend Visibility

When declaring a friend f of a class C, f is injected into the surrounding namespace, but not visible in the surrounding namespace: [cpp] class C { friend void f(); }; void g() { f(); } // error Declare f in the surrounding namespace, so that it is visible as intended: [cpp] void f(); class C { friend void f(); }; void g() { f(); } // ok

Escape Sequences

Only use valid escape sequences in character and string literals. For example, write [cpp] "/" and not [cpp] "\/"

Personal tools