使用返回的模板参数创建对象失败,为什么?

M.M*_*Mac 3 c++ templates

我想使用一个类中的模板参数,以实例化具有相同模板参数的另一个对象。我试图将参数保存在结构体中,但无法将类型用于新对象。

template<typename D>struct Typename {
    using myType = D;
};

template<typename T>
class example{

public:
    Typename<T> someType;
};

int main(){
        example<double> e1;//ok
        example<e1.someType.myType> e2; //error: cannot refer to member     'myType'
}
Run Code Online (Sandbox Code Playgroud)

错误信息:

error: invalid use of ‘using myType = double’
     example<e1.someType.myType> e2;
                         ^~~~~~
error: template argument 1 is invalid
     example<e1.someType.myType> e2;
                               ^
error: conflicting declaration ‘example<double> e2’
     example<typename decltype(e1.someType)::myType> e2; //error: cannot refer to member 'myType'
                                                     ^~
Run Code Online (Sandbox Code Playgroud)

Vit*_*meo 8

myType不是的数据成员Typename,因此您不能使用成员访问语法(.)。您需要从类型本身访问myType使用范围解析运算符(::)。例如

example<decltype(e1.someType)::myType>
Run Code Online (Sandbox Code Playgroud)