Functionisnotusableasa'constexpr'function
Take a look at the following code
#include <type_traits>
template <typename T>
struct basic_type {
using type = T;
};
consteval auto foo(auto p, auto x) noexcept {
if constexpr (p(x)) {
return 1;
} else {
return 0;
}
}
int main() {
// This compiles
return foo(
[]<typename T>(basic_type<T>)
{
return std::is_integral_v<T>;
},
basic_type<int>{});
// This gives "x is not a constant expression"
/*return foo(
[]<typename T>(T)
{
return std::is_integral_v<std::decay_t<T>>;
},
0);*/
}
The first return statement compiles just fine on latest gcc trunk, while the second one does not compile, with the error message:
source>: In instantiation of 'consteval auto foo(auto:1, auto:2) [with auto:1 = main()::<lambda(T)>; auto:2 = int]':
<source>:26:12: required from here
<source>:9:3: error: 'x' is not a constant expression
9 | if constexpr (p(x)) {
| ^~
<source>: In function 'int main()':
<source>:26:19: error: 'consteval auto foo(auto:1, auto:2) [with auto:1 = main()::<lambda(T)>; auto:2 = int]' called in a constant expression
26 | return foo(
| ~~~^
27 | []<typename T>(T)
| ~~~~~~~~~~~~~~~~~
28 | {
| ~
29 | return std::is_integral_v<std::decay_t<T>>;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30 | },
| ~~
31 | 0);
| ~~
<source>:8:16: note: 'consteval auto foo(auto:1, auto:2) [with auto:1 = main()::<lambda(T)>; auto:2 = int]' is not usable as a 'constexpr' function because:
8 | consteval auto foo(auto p, auto x) noexcept {
| ^~~
source>: In instantiation of 'consteval auto foo(auto:1, auto:2) [with auto:1 = main()::<lambda(T)>; auto:2 = int]':
<source>:26:12: required from here
<source>:9:3: error: 'x' is not a constant expression
9 | if constexpr (p(x)) {
| ^~
<source>: In function 'int main()':
<source>:26:19: error: 'consteval auto foo(auto:1, auto:2) [with auto:1 = main()::<lambda(T)>; auto:2 = int]' called in a constant expression
26 | return foo(
| ~~~^
27 | []<typename T>(T)
| ~~~~~~~~~~~~~~~~~
28 | {
| ~
29 | return std::is_integral_v<std::decay_t<T>>;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30 | },
| ~~
31 | 0);
| ~~
<source>:8:16: note: 'consteval auto foo(auto:1, auto:2) [with auto:1 = main()::<lambda(T)>; auto:2 = int]' is not usable as a 'constexpr' function because:
8 | consteval auto foo(auto p, auto x) noexcept {
| ^~~
Can anyone tell me why?
Here's a godbolt link
https://godbolt.org/z/71rbWob4e
EDIT
As requested, here's foo without auto parameters:
Error Message looks like this:
<source>: In instantiation of 'consteval auto foo(Predicate, T) [with Predicate = main()::<lambda(T)>; T = int]':
<source>:27:15: required from here
<source>:10:3: error: 'x' is not a constant expression
10 | if constexpr (p(x)) {
| ^~
<source>: In function 'int main()':
<source>:27:15: error: 'consteval auto foo(Predicate, T) [with Predicate = main()::<lambda(T)>; T = int]' called in a constant expression
27 | return foo(
| ~~~^
28 | []<typename T>(T)
| ~~~~~~~~~~~~~~~~~
29 | {
| ~
30 | return std::is_integral_v<std::decay_t<T>>;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31 | },
| ~~
32 | 0);
| ~~
<source>:9:16: note: 'consteval auto foo(Predicate, T) [with Predicate = main()::<lambda(T)>; T = int]' is not usable as a 'constexpr' function because:
9 | consteval auto foo(Predicate p, T x) noexcept {
|
回答
为了评估这一点:
if constexpr (p(x)) {
我们需要p(x)是一个常量表达式。某事是否符合常量表达式的规则基于您不允许做的事情列表。
When xis a basic_type<int>and pis a function that take a basic_type<int>by value, 根本没有我们违反的规则。这是一个空类型,因此复制它(正如我们在这里所做的那样)实际上并不涉及任何类型的读取。这只是有效。
但是 when xis an intandp是一个接受intby 值的函数,这也需要复制,x但这一次它涉及读取 的值x。因为,当然,必须以某种方式初始化参数。这确实违反了一条规则:[expr.const]/8说我们不允许执行:
左值到右值的转换,除非它应用于
- 引用可用于常量表达式的对象的非易失性泛左值,或
- 文字类型的非易失性泛左值,它指的是一个非易失性对象,其生命周期开始于 E 的求值内;
当我们读取一个变量的值时会发生左值到右值的转换,这两种情况都不适用。在这里,您实际上并不关心值是什么并不重要,因为p不使用它。为了能够甚至调用p,您必须复制x,并且不允许这样做。因此错误。
但是,这里的 lambda 实际上并不需要值,只需要类型,因此您可以改为这样写:
return foo(
[]<typename T>(T const&)
{
return std::is_integral_v<std::decay_t<T>>;
},
0);
现在我们不再复制x到 lambda 中,因为 lambda 不再按值获取 - 它按引用获取。因此,我们没有违反左值到右值转换规则(或任何其他规则),现在这是一个有效的常量表达式。
然后,作为奖励,如果您更改foo为x按引用获取(因为,您实际上并不关心值,所以为什么不关心):
consteval auto foo(auto p, auto const& x) noexcept {
if constexpr (p(x)) {
return 1;
} else {
return 0;
}
}
然后这两个变体都变得格式错误。无论是basic_type<int>和int版本(无论是否采取int通过值或引用)。有关这种情况的更多信息,请参阅我目前正在尝试使用P2280解决的 constexpr 数组大小问题。