开发者

C++ using C code using double underscores in defines and identifiers

开发者 https://www.devze.com 2023-03-21 06:51 出处:网络
I understand that in C++ double underscores in identifiers are reserved for the compiler. I have some C code which has character开发者_如何转开发istics similar to this in the corresponding header file

I understand that in C++ double underscores in identifiers are reserved for the compiler. I have some C code which has character开发者_如何转开发istics similar to this in the corresponding header files:

extern "C" {
    #define HELLO__THERE 1
    int hello__out__there( int );
}

I will be using this header in a C++ project, and plan to be doing things in C++ like:

if (HELLO__THERE == abc) 
    hello__out__there(foo);

Is this acceptable behavior in C++, covered by the standard?


In the C++03 standard 17.4.3.1.2 Global names, that use of underscores is defined as reserved:

Each name that contains a double underscore (_ _) or begins with an underscore followed by an upper- case letter (2.11) is reserved to the implementation for any use.

Being reserved means that it might be used in any conforming implementation and therefore it is not advisable to use it.


You should be fine, unless by some fluke chance that one of the defines has clashes with your compiler's one. If that is the case, it'll likely be a warning or error (depending on your compiler's configuration) that there'll be a duplicate symbol.

Hope it helps. Cheers!


The method call would be OK but why compare HELLO_THERE to some value abc? If you were testing to see if a method was there I would wrap it in #ifdef ... #endif instead because if hello_out_there is not defined for some reason that would be a compile error.


double underlines in identifiers are reserved for the compiler

First, it's underscore I guess. Second such identifiers are reserved. That doesn't hold one back to not use it. You can use it (until there is no naming conflict).

Is this acceptable behavior in C++, covered by the standard?

Yes. It's acceptable. However, there is difference between acceptable and good code. If you are following a proper coding guidelines then your code will be good as well as acceptable. IMHO, you should refer to some good coding standards on internet; it will help you a lot.

0

精彩评论

暂无评论...
验证码 换一张
取 消