Why does static_assert constexpr function not work in non-template struct but works free or in template?

Viewed 31

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?

0 Answers
Related