mercredi 2 décembre 2015

Definition different from declaration for constexpr static member

Consider the code

#include <iostream>

struct Foo
{
    constexpr static int n = 42;
};

const int Foo::n; // note const, NOT constexpr

int main()
{
    std::cout << Foo::n;
}

The definition of the static member is different from the in-class declaration, i.e. const is used instead of constexpr. Is the code above legal, and if yes, why? It compiles with both gcc and clang. It also compiles if we interchanged const and constexpr in the definition and declaration, respectively. I know that constexpr implies const on variables, but not the other way around.

Aucun commentaire:

Enregistrer un commentaire