4

I've seen quite a lot lately, in games or other applications that class data members, or methods or other stuff have "_" in front of the name. For example taken fro DXUT.cpp (Directx) _Acquires_lock_(g_cs) or _Releases_lock_(g_cs) or _tmain . There are numerous examples like this in game programming like there (Taken from GameFromScratch Tutorial)

  static GameState _gameState;
  static sf::RenderWindow _mainWindow;

These are just some data members of some type.

Is there any reason behind the _ char? Is if specifically for something?

3
  • _Acquires_lock_ and _Releases_lock_ are symbols used by the Microsoft compiler to annotate locking behavior. Since those are part of Microsoft's C++ implementation, it is legal (and desirable) to have them use reserved names. Commented Feb 11, 2016 at 14:14
  • 1
    In my experience as a game developer (14 years), many teams use this syntax to denote a symbol private to the local compilation unit, typically statics. It stems from a misunderstanding of the rules around _. Commented Feb 22, 2016 at 17:42
  • @kfsone thanks for that! and interesting info since it seems that the underscore is indeed misunderstood and sometimes used in inappropriate ways , as I found out ... Commented Feb 24, 2016 at 19:27

3 Answers 3

13

Usually, when you see a name with leading underscore, it either

  • belongs to the (C++) implementation, or

  • has been chosen by someone unaware of the first possibility.

It's not advisable to use names with leading underscore in user code.

Any name starting with underscore is reserved to the implementation in the global namespace, and any name starting with leading underscore followed by uppercase, is reserved to the implementation anywhere.

As I recall also any name with two successive underscores, is reserved.


A novice programmer may use leading underscore to indicate “data member”.

The usual convention, for those aware of the above, is instead a trailing underscore, and/or a prefix like m or my.

E.g. trailing underscore is, as I recall, used in Boost, while an m or my prefix is used (still as I recall) in MFC.

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

Comments

2

_Acquires_lock_(g_cs) or _Releases_lock_(g_cs)

That naming convention uses reserved names which means only the compiler implementation is allowed to use them.

That ensures that the implementation can use names which can never clash with macros or other names defined by users, because users are not allowed to use those reserved names.

The names with a single underscore followed by a lowercase letter are just idiomatic ways to denote a member variable, or a private implementation detail that is not part of the API.

Comments

-7

Usually an underscore(_) before the variable name means that it is a part of the C++ implementation on your platform. Avoid beginning variables with underscore in your own code.

Read the answers to "What are the rules about using an underscore in a C++ identifier?" for some more information about C++ naming conventions (especially the underscore one).

6 Comments

−1 Really Bad Advice™: “most developers would argue that you should follow [the leading underscore convention]". I would argue strongly that one should not use that convention, ever. It conflicts with the rules for reserved names in C and C++.
I do not believe most developers would argue that a single leading underscore is a good naming convention.
I'll go ahead and edit that! :)
I stay away from leading underscores. They are an invitation to mess something up.
@Todai I think this link would be better: stackoverflow.com/questions/228783/…
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.