Does using leading underscores actually cause trouble?

你离开我真会死。 提交于 2019-12-01 22:37:33
G.Y

The results may vary according to the specific complier you will use. Regarding the "danger level" - every time you'll get a bug - you will have to wonder if it is originates from your implemented logic or from the fact you are not using the standard.

But that is not all... let's assume someone tells you: "it is perfectly safe!" So, you can do that with no problem at all (only assuming..) Will it redefine your thinking when you get to a bug or still you will be wondering if there is a slight chace he was wrong? :)

So, you see, no matter which answer you will get it can never be a good one. (which makes me actually like your question)

I now care because I just encountered a failure with underscores, large and old codebase, mostly aimed at Windows and compiled with VS2005 but some is also cross-compiled to Linux. While analyzing updates to a newer gcc, I rebuilt some under cygwin just for ease of syntax checking. I got totally unintelligible errors (to my tiny brain) out of a line like:

template< size_t _N = 0 > class CSomeForwardRef;

That produced an error like:

error: expected ‘>’ before numeric constant

Google on that error turned up https://svn.boost.org/trac/boost/ticket/2245 and https://svn.boost.org/trac/boost/ticket/7203 both of which hinted that a stray #define could get in the way. Sure enough, an examination of the preprocessed source via -E and a hunt thru the include path turned up some bits-related .h (forget which) that defined _N. Later in that same odyssey I encountered a similar problem with _L.

Edit: Not bits-related but char-related: /usr/include/ctype.h -- here are some samples together with how ctype.h uses them:

#define _L      02
#define _N      04
.
.
.
#define isalpha(__c)    (__ctype_lookup(__c)&(_U|_L))
#define isupper(__c)    ((__ctype_lookup(__c)&(_U|_L))==_U)
#define islower(__c)    ((__ctype_lookup(__c)&(_U|_L))==_L)
#define isdigit(__c)    (__ctype_lookup(__c)&_N)
#define isxdigit(__c)   (__ctype_lookup(__c)&(_X|_N))

I'll be scanning the source for all underscored identifiers and weeding out via rename all those we created in error ...

Jon

Just how dangerously would I be living?
Dangerous enough to break your code in next compiler upgrade.

Think of the future, your code might not be portable and might break in future because future enhancement releases from your implementation might have exactly the same symbol name as you use.

Since the question has a pinch of: "It can be wrong yet how wrong can it be and when ever has it been wrong" flavor, I think Murphy's law answers this rather aptly:

"Anything that can go wrong will go wrong (When you are least expecting it)".[#]

[#] The (,) is my invention not Murphy's.

If you try to build your code somewhere where there's actually a conflict you will see strange build errors, or worse, no build error at all and incorrect runtime behavior.

I have seen someone use a reserved identifier which had to be changed when it caused build problems on a new platform.

It's not all that likely, but there's no reason to do it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!