0

From the following code:

struct Foo
{
    static constexpr int f() { return 42; }
    static_assert(f() == 42); // ERROR
};

I'm getting:

C2131: Expression did not evaluate to a constant

But the following works:

struct Foo
{
    static constexpr int f() { return 42; }
};

static_assert(Foo::f() == 42); // OK

and the following also works:

template<int x>
struct Foo
{
    static constexpr int f() { return 42; }
    static_assert(f() == 42); // OK
};

Any idea what's going on here?

Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
  • 1
    I once asked the exact same question here: [static constexpr member function not callable inside non-template class C++](https://stackoverflow.com/questions/53633322/static-constexpr-member-function-not-callable-inside-non-template-class-c). – Stack Danny Sep 20 '22 at 05:20
  • G++'s error is slightly more informative: ":4:20: error: `static constexpr int Foo::f()` called in a constant expression before its definition is complete" – Nathan Pierson Sep 20 '22 at 05:33

0 Answers0