Writing correct Cplusplus
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]
"\/"