// gcc: ok
// clang: compile error
struct arg
{
consteval arg(const int i) // consteval
: i_(i)
{
chk<int>(i); // error!!
}
template <typename T>
constexpr void chk(const T i) // template member
{}
int i_;
};
void f(arg i)
{}
int main()
{
f(10);
return 0;
}
demo: https://godbolt.org/z/cjjGz7xP9
I'm working with GCC and Clang in C++23.
When calling a constexpr template member function in a consteval constructor, Clang rejects the program. It compains:
<source>:39:7: error: call to consteval function 'arg::arg' is not a constant expression
39 | f(10);
| ^
<source>:23:9: note: undefined function 'chk<int>' cannot be used in a constant expression
23 | chk<int>(i);
| ^
I tested some modifications (see the demo). The code at the top is the one for #define case 2.
- case 1: If the
arg::argconstructor is changed toconstexpr, of course the errors are gone. - case 3: If
arg::chk()is changed to a non-template member, it compiles. - case 4: If
chk()is changed to a regular template function (not a member), it compiles.
If Clang's rejection is right, I wonder why the error occurs only when arg::chk() is a template and a member function, and why Clang says undefined function 'chk<int>'.
chkprior to constructor (let's call it case 5). godbolt.org/z/37xqcMnYx Also one should be careful with redefining keywords, such as#define case ....chkdefinition is visible.