C++使用 C 代码在定义和标识符中使用双下划线

发布于 2024-11-25 07:19:38 字数 324 浏览 2 评论 0原文

据我所知,在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一抹苦笑 2024-12-02 07:19:38

在 C++03 标准 17.4.3.1.2 全局名称 中,下划线的使用被定义为保留:

每个包含双下划线 (_ _) 或以下划线开头后跟大写字母的名称
大小写字母 (2.11) 保留给实现以供任何使用。

被保留意味着它可能用于任何符合要求的实现中,因此不建议使用它。

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.

只为守护你 2024-12-02 07:19:38

你应该没问题,除非碰巧某个定义与你的编译器的定义发生冲突。如果是这种情况,可能会出现警告或错误(取决于编译器的配置),表明存在重复符号。

希望有帮助。干杯!

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!

故事未完 2024-12-02 07:19:38

方法调用没问题,但为什么要将 HELLO_THERE 与某个值 abc 进行比较呢?如果您正在测试某个方法是否存在,我会将其包装在 #ifdef ... #endif 中,因为如果由于某种原因未定义 hello_out_th​​ere ,则会出现编译错误。

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.

茶底世界 2024-12-02 07:19:38

标识符中的双下划线是为编译器保留的

首先,我猜它是下划线。其次,此类标识符被保留。这并不妨碍人们不使用它。您可以使用它(直到不存在命名冲突)。

这是标准涵盖的 C++ 中可接受的行为吗?

是的。这是可以接受的。然而,可接受的代码和好的代码之间是有区别的。如果您遵循正确的编码指南,那么您的代码将会很好并且可以接受。恕我直言,您应该参考互联网上一些好的编码标准;它会对你有很大帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文