13
// 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::arg constructor is changed to constexpr, 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>'.

10
  • You can make code compile by defining chk prior to constructor (let's call it case 5). godbolt.org/z/37xqcMnYx Also one should be careful with redefining keywords, such as #define case .... Commented Apr 6 at 9:35
  • 3
    Maybe this has something to do with invocation of an constexpr function which is not defined yet not being a core constant expression as defined in [expr.const]. Commented Apr 6 at 9:46
  • 1
    @Swift-FridayPie namespace shouldn't have any effect, but template delays the instantiation to invocation time, when chk definition is visible. Commented Apr 6 at 13:24
  • 3
    @Red.Wave In particular, it's a version of CWG2335. Commented Apr 6 at 18:27
  • 1
    It looks similar to this Clang issue: github.com/llvm/llvm-project/issues/73232 Commented Apr 7 at 7:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.