Search code examples
c++compiler-errorsc++20constexprconsteval

Why does `consteval` not behave as expected?


inline consteval unsigned char operator""_UC(const unsigned long long n)
{
    return static_cast<unsigned char>(n);
}

inline consteval char f1(auto const octet)
{
    return char(octet >> 4_UC);
}

inline constexpr char f2(auto const octet)
{
    return char(octet >> 4_UC);
}

int main()
{
    auto c1 = f1('A'); // ok
    auto c2 = f2('A'); // error

    return c1 + c2;
}

Compiled with: clang++-14 -std=c++20 -stdlib=libc++, and the error message is:

undefined reference to `operator"" _UC(unsigned long long)'

See online demo

Why does f1() is ok while f2() is not?

Why does consteval not behave as expected?


Solution

  • This is a bug of clang 14 and has been fixed.

    See: https://github.com/llvm/llvm-project/commit/ca844ab01c3f9410ceca967c09f809400950beae