It's common in C++ to name member variables with some kind of prefix to denote the fact that they're member variables, rather than local variables or parameters. If you've come from an MFC background, you'll probably use m_foo
. I've also seen myFoo
occasionally.
C# (or possibly just .NET) seems to recommend using just an underscore, as in _foo
. Is this allowed by the C++ standard?
Best Answer
The rules (which did not change in C++11):
std
namespace is reserved. (You are allowed to add template specializations, though.)From the 2003 C++ Standard:
The C++ language is based on the C language (1.1/2, C++03), and C99 is a normative reference (1.2/1, C++03), so it's useful to know the restrictions from the 1999 C Standard (although they do not apply to C++ directly):
Other restrictions might apply. For example, the POSIX standard reserves a lot of identifiers that are likely to show up in normal code:
E
followed a digit or uppercase letter:is
orto
followed by a lowercase letterLC_
followed by an uppercase letterf
orl
are reservedSIG
followed by an uppercase letter are reservedSIG_
followed by an uppercase letter are reservedstr
,mem
, orwcs
followed by a lowercase letter are reservedPRI
orSCN
followed by any lowercase letter orX
are reserved_t
are reservedWhile using these names for your own purposes right now might not cause a problem, they do raise the possibility of conflict with future versions of that standard.
Personally I just don't start identifiers with underscores. New addition to my rule: Don't use double underscores anywhere, which is easy as I rarely use underscore.
After doing research on this article I no longer end my identifiers with
_t
as this is reserved by the POSIX standard.The rule about any identifier ending with
_t
surprised me a lot. I think that is a POSIX standard (not sure yet) looking for clarification and official chapter and verse. This is from the GNU libtool manual, listing reserved names.CesarB provided the following link to the POSIX 2004 reserved symbols and notes 'that many other reserved prefixes and suffixes ... can be found there'. The POSIX 2008 reserved symbols are defined here. The restrictions are somewhat more nuanced than those above.