Re: Names beginning with _ or __
Quote:
Originally Posted by Hermit
Though, now that I think of it, macros have the potential to conflict with a compiler's implementation of the standard library no matter what, so maybe that's why there's no specific stipulation regarding macros.
But there is. The std states,
Quote:
1 A translation unit that includes a standard library header shall not #define or #undef names declared in any standard
library header.
2 A translation unit shall not #define or #undef names lexically identical to keywords.
It seems that the std's committee was just trying to establish boundaries to prevent naming conflicts. You can name a variable like so:
Code:
int main()
{
int __success;
__success = callAFunction();
return success;
}
You do so at your own risk. Global names beginning with __ are reserved to the implementation. In other words, there may be no conflict and the above code may compile and work just fine. If the compiler writers prefix their variables with "__" it just makes it easy to avoid conflicts. I'm sure they had reasons for defining different rules for MACROS but I have no idea what those reasons would be. Just follow the guideline in the std and you'll stay out of trouble. Also, avoid writing macros and global variables and functions as much as possible. If you are writing object oriented C++ this issue shouldn't be a big deal.
The std also states the following:
"If the program declares or defines a name in a context where it is reserved, other than as explicitly allowed by this clause,
the behavior is undefined."
You may not get a compiler error if you use a reserved name but the results are undefined. Anyway, the std seems pretty clear to me now that I have read it. Not sure if there is anything left to discuss at this point.
Re: Names beginning with _ or __
If you really want to use a leading underscore, do this..
Code:
int _PREFIX_THEY_WILL_NOT_USE__MyFunctionName();
Re: Names beginning with _ or __
Can I just point out that the double underscore is reserved for anywhere in the name, not just at the start.
Quote:
Originally Posted by C++Standard 17.4.3.1.2/1 Global names
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 uppercase 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.
Also, just because the variable you want to name with a leading underscore isn't global doesn't mean that you should do it. You may find that you've hidden an important global variable by doing that. Now, that one could be an interesting debugging exercise.
The simplest rule to remember is "never start a name with an underscore and never use double underscores". It's overkill, but so what? Are leading and double underscores that important?
Re: Names beginning with _ or __
Quote:
Originally Posted by Graham
Can I just point out that the double underscore is reserved for anywhere in the name, not just at the start.
Also, just because the variable you want to name with a leading underscore isn't global doesn't mean that you should do it. You may find that you've hidden an important global variable by doing that. Now, that one could be an interesting debugging exercise.
The simplest rule to remember is "never start a name with an underscore and never use double underscores". It's overkill, but so what? Are leading and double underscores that important?
I do not use underscores very often, and when I do it's not at the front. The only time I ever use an underscore in practice at the front, is when I implement _DllMainCRTStartup to bypass the C runtime.