C++使用 C 代码在定义和标识符中使用双下划线
据我所知,在 C++ 中,标识符中的双下划线是为编译器保留的。我有一些 C 代码,在相应的头文件中具有与此类似的特征:
extern "C" {
#define HELLO__THERE 1
int hello__out__there( int );
}
我将在 C++ 项目中使用此头文件,并计划在 C++ 中做一些事情,例如:
if (HELLO__THERE == abc)
hello__out__there(foo);
这是标准涵盖的 C++ 中可接受的行为吗?
I understand that in C++ double underscores in identifiers are reserved for the compiler. I have some C code which has characteristics 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 C++03 标准
17.4.3.1.2 全局名称
中,下划线的使用被定义为保留:被保留意味着它可能用于任何符合要求的实现中,因此不建议使用它。
In the C++03 standard
17.4.3.1.2 Global names
, that use of underscores is defined as reserved: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!
方法调用没问题,但为什么要将 HELLO_THERE 与某个值 abc 进行比较呢?如果您正在测试某个方法是否存在,我会将其包装在 #ifdef ... #endif 中,因为如果由于某种原因未定义 hello_out_there ,则会出现编译错误。
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.
首先,我猜它是下划线。其次,此类标识符被保留。这并不妨碍人们不使用它。您可以使用它(直到不存在命名冲突)。
是的。这是可以接受的。然而,可接受的代码和好的代码之间是有区别的。如果您遵循正确的编码指南,那么您的代码将会很好并且可以接受。恕我直言,您应该参考互联网上一些好的编码标准;它会对你有很大帮助。
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).
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.