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 {
| ^~~
Can anyone tell me why?
Here's a godbolt link https://godbolt.org/z/71rbWob4e
EDIT
As requested, here's foo without auto parameters:
template<typename Predicate, typename T>
consteval auto foo(Predicate p, T x) noexcept {
if constexpr (p(x)) {
return 1;
} else {
return 0;
}
}
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 {
|
0
vsbasic_type<int>{}
notstd::decay_t<T>
vsT
(godbolt.org/z/5Mfe14MxW) – largest_prime_is_463035818 Mar 30 at 11:08if constexpr
I'm inclined to believe that the error is correct (but not 100% sure). Can you try to rewrite your foo replacingauto
parameters with actual template arguments? Likely it'd make it clearer. – Dan M. Mar 30 at 11:18consteval
andconstexpr
function can have non-compile-time arguments. It means that compiler makes most general assumptions when compiling this function. And this assumption is that arguments are non-compile-time. It means that compiler can't guarantee that yourif-constexpr
is always compile-time, hence the error about it. If you place all your arguments as templateauto
parameters of your function then it should compile well. – Arty Mar 30 at 12:31