How to test if preprocessor symbol is #define'd but has no value?

前端 未结 9 1393
渐次进展
渐次进展 2020-11-30 06:08

Using C++ preprocessor directives, is it possible to test if a preprocessor symbol has been defined but has no value? Something like that:

#define MYVARIABLE         


        
9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 06:56

    I want to make sure that the project fails to compile if user forgot to define this string.

    While i'd check this in a previous build-step, you can do this at compile-time. Using Boost for brevity:

    #define A "a"
    #define B
    BOOST_STATIC_ASSERT(sizeof(BOOST_STRINGIZE(A)) > 1); // succeeds
    BOOST_STATIC_ASSERT(sizeof(BOOST_STRINGIZE(B)) > 1); // fails
    

提交回复
热议问题