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?

Sabyc90
  • 133
  • 3
  • 10
  • `_Acquires_lock_` and `_Releases_lock_` are symbols used by the Microsoft compiler to [annotate locking behavior](https://msdn.microsoft.com/en-us/library/hh916381.aspx). Since those are part of Microsoft's C++ implementation, it is legal (and desirable) to have them use reserved names. – IInspectable Feb 11 '16 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 `_`. – kfsone Feb 22 '16 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 ... – Sabyc90 Feb 24 '16 at 19:27

3 Answers3

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.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
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.

Community
  • 1
  • 1
Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
-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).

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Todai
  • 164
  • 9
  • 2
    I do not believe most developers would argue that a single leading underscore is a good naming convention. – Jonathan Wakely Feb 11 '16 at 13:48
  • 2
    **−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++. – Cheers and hth. - Alf Feb 11 '16 at 13:48
  • I'll go ahead and edit that! :) – Todai Feb 11 '16 at 13:49
  • I stay away from leading underscores. They are an invitation to mess something up. – NathanOliver Feb 11 '16 at 13:49
  • @Todai I think this link would be better: http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier – Simon Kraemer Feb 11 '16 at 13:53
  • Yeesh, for a simple answer it's painfully down-voted (when it could've been ignored instead). But that was in 2016, anyway – Lapys Jul 01 '20 at 13:33