有没有办法让 C 预处理器解析 #error 语句中的宏?

发布于 2024-11-07 01:57:02 字数 537 浏览 0 评论 0原文

正如标题所说。我想在 #error 语句的文本中使用预处理器宏:

#define SOME_MACRO 1

#if SOME_MACRO != 0
    #error "SOME_MACRO was not 0; it was [value of SOME_MACRO]"
#endif

在此示例中,我希望预处理器将 [value of SOME_MACRO] 解析为 SOME_MACRO 的实际值在本例中为 1。这应该在预处理器、编译器或任何进程 #error 打印错误输出之前发生
有没有办法做到这一点,或者这是不可能的?

我不想知道是否有 ISO C++ 标准方法可以做到这一点,因为据我所知,任何 ISO C++ 标准中都没有声明预处理器指令 #error 。不过,我知道 GCC 和 Visual C++ 支持 #error。但我的问题并不是特定于这些编译器,我只是好奇是否有任何 C/C++ 编译器/预处理器可以做到这一点。

我尝试搜索该主题,但没有成功。

Just as the title says. I want to use a preprocessor macro in the text of an #error statement:

#define SOME_MACRO 1

#if SOME_MACRO != 0
    #error "SOME_MACRO was not 0; it was [value of SOME_MACRO]"
#endif

In this example I want the preprocessor to resolve [value of SOME_MACRO] to the actual value of SOME_MACRO which in this case is 1. This should happen before the preprocessor, compiler or whatever processes #error prints the error output
Is there a way to do that or is this just not possible?

I don't want to know if there is an ISO C++ standard way to do that, because afaik the preprocessor directive #error is not stated in any ISO C++ standard. However, I know GCC and Visual C++ support #error. But my question is not specific to those compilers, I'm just curious if any C/C++ compiler/preprocessor can do that.

I tried to search for that topic but without any luck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

暮光沉寂 2024-11-14 01:57:02

为了完整性,我建议的 C++0x 方式(使用与 Kirill 相同的技巧):

#define STRING2(x) #x
#define STRING(x) STRING2(x)

#define EXPECT(v,a) static_assert((v)==(a), "Expecting " #v "==" STRING(a) " [" #v ": "  STRING(v) "]")


#define VALUE 1

EXPECT(VALUE, 0);

给出:

g++ -Wall -Wextra -std=c++0x test.cc                     
test.cc:9: error: static assertion failed: "Expecting VALUE==0 [VALUE: 1]"

For completeness the C++0x way I suggested (using the same trick as Kirill):

#define STRING2(x) #x
#define STRING(x) STRING2(x)

#define EXPECT(v,a) static_assert((v)==(a), "Expecting " #v "==" STRING(a) " [" #v ": "  STRING(v) "]")


#define VALUE 1

EXPECT(VALUE, 0);

Gives:

g++ -Wall -Wextra -std=c++0x test.cc                     
test.cc:9: error: static assertion failed: "Expecting VALUE==0 [VALUE: 1]"
不念旧人 2024-11-14 01:57:02

在 Visual Studio 中,您可以使用 pragmamessage< /code>如下:

#define STRING2(x) #x
#define STRING(x) STRING2(x)

#define SOME_MACRO 1

#if SOME_MACRO != 0
    #pragma message ( "SOME_MACRO was not 0; it was " STRING(SOME_MACRO) )
    #error SOME_MACRO was not 0;
#endif

这将生成两条消息,但您将获得 SOME_MACRO 的值。在 G++ 中,请使用以下代码(来自注释:g++ 版本 4.3.4 可以很好地使用括号,如上面的代码所示):

#pragma message "SOME_MACRO was not 0; it was " STRING(SOME_MACRO)

In Visual Studio you can use pragmamessage as follows:

#define STRING2(x) #x
#define STRING(x) STRING2(x)

#define SOME_MACRO 1

#if SOME_MACRO != 0
    #pragma message ( "SOME_MACRO was not 0; it was " STRING(SOME_MACRO) )
    #error SOME_MACRO was not 0;
#endif

This will generate two messages, but you'll get the value of SOME_MACRO. In G++ use the following instead (from comments: g++ version 4.3.4 works well with parenthesis as in the code above):

#pragma message "SOME_MACRO was not 0; it was " STRING(SOME_MACRO)
瑕疵 2024-11-14 01:57:02
#define INVALID_MACRO_VALUE2(x) <invalid_macro_value_##x>
#define INVALID_MACRO_VALUE(x) INVALID_MACRO_VALUE2(x)

#if SOME_MACRO != 0
  #include INVALID_MACRO_VALUE(SOME_MACRO)
#endif

在 Visual Studio 2005 中生成“无法打开包含文件:'invalid_macro_value_1':没有这样的文件或目录”,并且在其他编译器上可能会生成类似的消息。

这并没有直接回答您有关使用 #error 的问题,但结果是相似的。

#define INVALID_MACRO_VALUE2(x) <invalid_macro_value_##x>
#define INVALID_MACRO_VALUE(x) INVALID_MACRO_VALUE2(x)

#if SOME_MACRO != 0
  #include INVALID_MACRO_VALUE(SOME_MACRO)
#endif

generates "Cannot open include file: 'invalid_macro_value_1': No such file or directory" in Visual Studio 2005 and probably similar messages on other compilers.

This doesn't answer your question directly about using #error, but the result is similar.

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