测试C宏的值是否为空 [英] Test if a C macro's value is empty

查看:777
本文介绍了测试C宏的值是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一些代码来验证宏是否已定义,但是为空(没有任何值)。

I need to write some code to verify that a macro is defined but empty (not having any values). The test does not need to be in compile time.

我试图写:

#if (funcprototype == "")
MY_WARN("funcprototype is empty");
#endif

代码不编译,因为 funcprototype 展开为空。

the code does not compile, as funcprototype expands to empty.

推荐答案

如果运行时检查可以,的字符串替换:

If a run-time check is okay, then you can test the length of the stringized replacement:

#define REAL_STRINGIZE(x) #x
#define STRINGIZE(x) REAL_STRINGIZE(x)

if (STRINGIZE(funcprototype)[0] == '\0') {
    // funcprototype expanded to an empty replacement list
}
else {
    // funcprototype expanded to a non-empty replacement list
}

我不认为有一个一般的情况这个宏被一个空的序列的令牌替换编译时检查。这是一个类似的问题,有可能比较两个令牌序列的相等性,这是不可能在编译时做的。

I don't think there is a general-case "is this macro replaced by an empty sequence of tokens" compile-time check. That is a similar problem to "is it possible to compare two sequences of tokens for equality," which is impossible to do at compile-time.

这篇关于测试C宏的值是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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