10

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 
};
gd1
  • 11,300
  • 7
  • 49
  • 88

1 Answers1

5

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;
gd1
  • 11,300
  • 7
  • 49
  • 88
Rafal Mielniczuk
  • 1,322
  • 7
  • 11
  • +1 Nice answer. Wondering how Z::C is available, then. – gd1 Sep 17 '14 at 10:18
  • 1
    I editet my answer, I think it is about noElems being inline, which makes the compiler to parse it after the cyz's declarations, while enum E is available, that's why cyz1 works – Rafal Mielniczuk Sep 17 '14 at 10:27