Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I pass a pointer to a consteval function as an NTTP in C++?

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

like image 522
Noam Elul Avatar asked Nov 23 '25 08:11

Noam Elul


1 Answers

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)

like image 161
Brian Bi Avatar answered Nov 24 '25 22:11

Brian Bi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!