10

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)?

Community
  • 1
  • 1
Patrick B.
  • 11,773
  • 8
  • 58
  • 101

3 Answers3

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

Community
  • 1
  • 1
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • 8
    +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. – jalf Nov 02 '11 at 11:53
  • 1
    Completely agree with @jalf. If you want, use a different prefix, like `m_`... – David Rodríguez - dribeas Nov 02 '11 at 11:58
  • 3
    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. – James Kanze Nov 02 '11 at 12:22
  • @Kanze Google C++ style guide recommends that class data members end with an underscore (https://google.github.io/styleguide/cppguide.html#Variable_Names). I think it's ok. – Justme0 Mar 16 '18 at 13:08
  • 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. – QuentinUK Mar 26 '23 at 12:15
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.)

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Offirmo
  • 18,962
  • 12
  • 76
  • 97
  • 3
    @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. – Offirmo Jan 19 '12 at 09:41
  • I really doubt Charles Simonyi's creations can be "bad", btw (may be to those not thinking, though) – mlvljr Jan 19 '12 at 14:21
  • 1
    @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. – akaltar Apr 05 '15 at 11:51
  • 2
    I like this better without the underscores, pFoo, mFoo, gFoo – paulm Jul 04 '16 at 13:56
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.

Community
  • 1
  • 1
Werner Henze
  • 16,404
  • 12
  • 44
  • 69