关于c ++:嵌套类是类模板中的依赖类型?

Nested classes are dependent types in class templates?

考虑下面的:P></

1
2
3
4
5
6
7
8
9
10
11
template<class> struct T
{
    struct T1
    {
        struct T2
        {

        };
    };
    /*typename*/ T1::T2 m;
};

没有typename磁带T2是失败的事情考虑,在AS和依赖thus not a name,type。后看到C + +标准草案(17,n4659)矛盾:为什么我相信我P></

§17.6.2.1?5P></

A name is a member of the current instantiation if it is
— An unqualified name that, when looked up, refers to at least one member of a class that is the current instantiation or a non-dependent base class thereof.
...
A name is a dependent member of the current instantiation if it is a member of the current instantiation that, when looked up, refers to at least one member of a class that is the current instantiation.

T1是依赖人of the current实例化。T2 is not of the current成员实例化(EN是T1,学院成员)P></

§17.6.2.1?9.3P></

A type is dependent if it is
...
— a nested class or enumeration that is a dependent member of the current instantiation,
...

T1是在套式舱和thus依赖型。P></

§17.6?3P></

When a qualified-id is intended to refer to a type that is not a member of the current instantiation (17.6.2.1) and its nested-name-specifier refers to a dependent type, it shall be prefixed by the keyword typename, forming a typename-specifier. ...

thus,typenameis needed。P></

我的理解是正确的吗?如果我知道这背后的理由,什么茶?我永远T1::T2of how the other查找find anything在T2比T1套式?P></


是的,你是对的。

在您的例子中,这并不重要,因为您不可能在不实例化EDOCX1(因为它是类成员)的情况下专门化T1。但你可以这样做,把m放在一个函数中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template<class> struct T
{
    struct T1
    {
        struct T2
        {

        };
    };

    void foo() {
        typename T1::T2 m;
    }
};

template<>
struct T<void>::T1 {
    int T2;
};

如果T1不依赖,您的代码可能会改变含义,因为T2引用的是值而不是类型。