【发布时间】:2020-11-02 12:47:52
【问题描述】:
在constexpr 函数中,我无法在受C++20 的std::is_constant_evaluated() 条件的if 语句的分支内定义非文字变量? Clang 和 GCC 都表示不允许,但在下面的示例中,允许在编译时无法评估的其他构造。对非字面量的使用有具体限制吗?
#include <type_traits>
struct Foo {
~Foo() {}
};
void non_constexpr() {}
constexpr bool bar()
{
if (std::is_constant_evaluated()) {
} else {
non_constexpr();
double d;
reinterpret_cast<int*>(&d);
Foo f; // error: variable ‘f’ of non-literal type ‘Foo’ in ‘constexpr’ function
}
return true;
}
constexpr bool x = bar();
【问题讨论】: