相关疑难解决方法(0)

用纯 C++ 实现 Linux 内核的 __is_constexpr (ICE_P) 宏

阅读了Martin Uecker 谓词的标准 C11 版本后ICE_P,我尝试用纯 C++ 实现它。C11版本,使用_Generic选择如下:

#define ICE_P(x) _Generic((1? (void *) ((x)*0) : (int *) 0), int*: 1, void*: 0)
Run Code Online (Sandbox Code Playgroud)

C++ 的明显方法是_Generic用模板 和替换decltype,例如:

template<typename T> struct is_ice_helper;
template<> struct is_ice_helper<void*> { enum { value = false }; };
template<> struct is_ice_helper<int*>  { enum { value = true  }; };

#define ICE_P(x) (is_ice_helper<decltype(1? (void *) ((x)*0) : (int *) 0)>::value)
Run Code Online (Sandbox Code Playgroud)

然而,它未能通过最简单的测试。为什么它不能检测整数常量表达式?

c++ language-lawyer constant-expression

6
推荐指数
1
解决办法
526
查看次数

标签 统计

c++ ×1

constant-expression ×1

language-lawyer ×1