有没有办法让C Preprocessor在#error语句中解析宏? [英] Is there a way to have the C Preprocessor resolve macros in an #error statement?

查看:80
本文介绍了有没有办法让C Preprocessor在#error语句中解析宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

在这个例子中,我希望预处理器将 [SOME_MACRO]的值解析为 SOME_MACRO 在这种情况下是1.这应该发生在预处理器,编译器或任何进程之前#error打印错误输出

是否有办法做到这一点,只是不可能?

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?

我不想知道是否有一个ISO C ++标准的方式来做,因为afaik预处理指令错误未在任何ISO C ++标准中说明。但是,我知道GCC和Visual C ++支持 #error 。但我的问题不是特定于那些编译器,我只是好奇,如果任何C / C ++编译器/预处理器可以这样做。

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.

我试图搜索该主题,

推荐答案

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

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);

赠送:

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

这篇关于有没有办法让C Preprocessor在#error语句中解析宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆