Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange constexpr behaviour for inner class

Can anybody try to explain this?

template<typename T, size_t S = T::noElems()>
struct C
{
};

struct X
{
    enum E { A, B, C };
    static constexpr size_t noElems() { return C+1; };
};

struct K
{
    C<X> cx; // this DOES compile
};

struct Y
{
    struct Z
    {
        enum E { A, B, C };
        static constexpr size_t noElems() { return C+1; };
    };
    C<Z, Z::C+1> cyz; // this DOES compile

    C<Z> cyz; // <--- this does NOT compile 
};
like image 914
gd1 Avatar asked Sep 17 '14 09:09

gd1


1 Answers

With the declaration of the struct

struct Y
{
    struct Z
    {
        enum E { A, B, C };
        static constexpr size_t noElems() { return C+1; };
    };
    C<Z, Z::C+1> cyz1; // this DOES compile

    C<Z> cyz2; // <--- this does NOT compile 
};

entities cyz1 and cyz2 are parsed before inline declaration of Z::noElems(), so the definition of

static constexpr size_t noElems() { return C+1; };

is not available at the time of declaration of

C<Z> cyz2;
like image 200
Rafal Mielniczuk Avatar answered Oct 18 '22 22:10

Rafal Mielniczuk