C++ Naming Standards – Do Underscore Prefixes Puzzle the Compiler?

cnaming-standardsprogramming practices

I've been taught since high school that defining variables like this:

int _a;

or

int __a;

should be consider bad practice because this would eventually puzzle compilers that use variables starting with an underscore to name temporary variables.

As far as I know this is the reason why some people like to move the underscore at the end of the name, like:

int a_;

However, I see a lot of code all around that makes use of underscore-starting variables. And that code builds fairly well with both Visual Studio 2010 and g++ 4.x.

So I wonder: is this a non-issue nowadays? Are modern compilers smarter about naming conventions?

Best Answer

You are apparently misunderstanding the reason prefix underscores are bad practice. To make it short, it is because the C and C++ standard reserve these prefix for implementation details, for example for standard library implementation. (note that _ and __ are not reserved for the same things, see comments)

Even if the names are under scope (namespace, class, etc.), there can be some global names, in particular macros, which use these prefix and might silently break your code if you use them too.

So, basically, most of the time it is safe to use these prefix BUT if you don't use them you have a 100% guarantee that your naming will never conflict with the implementation names.

Which is why, in doubt, don't use these prefix.