Is it legal to pass a pointer to a consteval function as an NTTP? Assuming that the pointer doesn't escape to runtime.
Given the following code, GCC trunk compiles it but Clang trunk rejects it:
#include <iostream>
consteval int f()
{
return 15;
}
template<auto V>
consteval int g()
{
return V();
}
int main()
{
constexpr int i = g<f>();
std::cout << i << std::endl;
}
godbolt link
No, you cannot. A constant template parameter (formerly known as non-type template parameter) must be a constant expression. [expr.const]/22.2.3 states that a prvalue constant expression cannot have a constituent value that is a pointer to an immediate function. For a prvalue of pointer type, the sole constituent value is the pointer value itself (see [expr.const]/2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With