12

In our projects we decided to prefix member variables and some private/protected methods with underscore (so with “_”).

During a discussion it was claimed that this is discouraged to do because of some incompatibilities with some compilers/linkers on some platforms. As we want to be a portable as possible I'd like to be sure.

I also reckon that prefixing globals with underscores in C can be a problem.

Does the same apply to C++-linkage and if so, in which cases (platforms/compilers/linkers)?

0

3 Answers 3

13

From the C++03 standard: §17.4.3.1.2/1

Certain sets of names and function signatures are always reserved to the implementation:

  • Each name that contains a double underscore (__) or begins with an underscore followed by an upper-case letter (2.11) is reserved to the implementation for any use.

  • Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.

The equivalent text is present in C++11 §17.6.4.3.2/1

Sign up to request clarification or add additional context in comments.

5 Comments

+1. In other words, the OP's usage is safe, as long as those members do not start with a capital letter. But I'd recommend against leading underscores in general, because it's too easy to accidentally break one of the rules shown here.
Completely agree with @jalf. If you want, use a different prefix, like m_...
Those are the official rules. In the past, I've had problems with macros in system header files also matching an names starting with an underscore followed by a small letter. And as a general rule, for readability, avoid underscore at either end of a symbol.
@Kanze Google C++ style guide recommends that class data members end with an underscore (google.github.io/styleguide/cppguide.html#Variable_Names). I think it's ok.
Google then recommends starting konstants with a k, including all enums even class enums! And also member access functions lowercase without the underscore but other member functions beginning with an uppercase. Which means if you change the internal implementation you have to change the interface.
6

Personally, I use m_name, with 'm' standing for 'member'.

By the way, I also use p_ for parameters in my functions and g_ for the few unavoidable globals.

(then I usually get bashed by SO users because it looks like Hungarian notation ;-) But it is not.)

4 Comments

@mlvljr thank for this constructive comment helping me and the community ;)This is NOT hungarian notation, but is mistaken as so by people who were taught "hungarian notation is bad" and apply this rule without thinking.
I really doubt Charles Simonyi's creations can be "bad", btw (may be to those not thinking, though)
@Offirmo I think this is the real hungarian notation. Just at the early times people misused it do show the type of the variable instead of the units it is in(meter vs cm), the latter of which is very helpful.
I like this better without the underscores, pFoo, mFoo, gFoo
2

Please also look here: What are the rules about using an underscore in a C++ identifier?

I've seen a lot of code using single underscores as a prefix or double underscores in an indentifier and it simply worked. But you never know. The identifiers are reserved and anything may happen, depending on the compiler.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.