有人可以试着解释一下吗?
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
};
答案 0 :(得分:5)
声明结构
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
};
实体cyz1
和cyz2
在内联声明Z::noElems()
之前进行解析,因此
static constexpr size_t noElems() { return C+1; };
在宣布时不可用
C<Z> cyz2;