简体   繁体   English

带有__has_include的_Static_assert

[英]_Static_assert with __has_include

I wanna make macro, that checks that file is "includable". 我想制作宏,检查该文件是否“可包含”。 Something like this: 像这样的东西:

#define CHECK(__file)    \
    _Static_assert( __has_include(__file), "Error")
CHECK("header.h");

But it produces errors like 但它产生的错误就像

f.c:28:3: __has_include must be used within a preprocessing directive
f.c:28:3: Implicit declaration of function '__has_include' is invalid in C99
f.c:28:3: Static_assert expression is not an integral constant expression

Is this even possible? 这甚至可能吗? Or maybe there is different solution that will give similar result? 或者也许有不同的解决方案可以给出类似的结果?

Thanks for any help! 谢谢你的帮助!

If all fails (and you can hardly argue with: "__has_include must be used within a preprocessing directive"), throw another layer of indirection at it: 如果全部失败(并且您很难争辩:“__ has_include必须在预处理指令中使用”),则在其上抛出另一层间接:

h_assert(){
    for h; do
printf '
#if !__has_include("%s")
_Static_assert(0,"__has_include(\"%s\")");
#endif
' "$h" "$h"
    done
}
h_assert foo.h bar.h baz.h #Generate the header assertions with a POSIX shell

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM