constexpr 函数中的非文字(通过 std::is_constant_evaluated)
Non-literal within a constexpr function (via std::is_constant_evaluated)
在 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();
有一个特定的限制。在这种情况下,关于 constexpr
函数体的结构限制。
[dcl.constexpr]
3 The definition of a constexpr function shall satisfy the
following requirements:
- its function-body shall not enclose
- a definition of a variable of non-literal type or of static or thread storage duration.
总而言之。如果你想知道为什么会这样,假设在不断的评估中不会执行任何禁止的代码,那也是一个 good question。不幸的是,我目前不知道 that 的答案。这可能只是还没有人想过要改变的东西。
在 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();
有一个特定的限制。在这种情况下,关于 constexpr
函数体的结构限制。
[dcl.constexpr]
3 The definition of a constexpr function shall satisfy the following requirements:
- its function-body shall not enclose
- a definition of a variable of non-literal type or of static or thread storage duration.
总而言之。如果你想知道为什么会这样,假设在不断的评估中不会执行任何禁止的代码,那也是一个 good question。不幸的是,我目前不知道 that 的答案。这可能只是还没有人想过要改变的东西。