使用type=smth。和模板类型 我在一段C++代码中遇到了一些麻烦。

使用type=smth。和模板类型 我在一段C++代码中遇到了一些麻烦。,c++,templates,c++11,using,C++,Templates,C++11,Using,首先,如果我这样做,它工作得很好: struct A { using my_type1 = double; using my_type2 = int; }; struct B { using size_type = A::my_type2; }; 但是,我希望能够选择我的_类型1,因此我选择了模板方式: template <typename T> struct A { using my_type1 = T; using my_type2 =

首先,如果我这样做,它工作得很好:

struct A
{
    using my_type1 = double;
    using my_type2 = int;
};

struct B
{
    using size_type = A::my_type2;
};
但是,我希望能够选择我的_类型1,因此我选择了模板方式:

template <typename T>
struct A
{
    using my_type1 = T;
    using my_type2 = int;
};

template <typename T>
struct B
{
    using size_type = A<T>::my_type2;
};
模板
结构A
{
使用我的_type1=T;
使用my_type2=int;
};
模板
结构B
{
使用size\u type=A::my\u type2;
};
在这里,gcc失败,行中有“预期的类型说明符”

using size_type = A<T>::my_type2;
使用size\u type=A::my\u type2;
我也可以把我的_type2放在模板中,但这是一个不会有太大变化的类型


那么为什么我的方法不起作用呢?谢谢

您必须添加
typename

using size_type = typename A<T>::my_type2;
使用size\u type=typename A::my\u type2;

您必须添加
typename

using size_type = typename A<T>::my_type2;
使用size\u type=typename A::my\u type2;

Templates无论如何都不允许您在运行时选择类型。@n.m:是的,这不是我的意思,抱歉。Templates无论如何也不允许您在运行时选择类型。@n.m:是的,这不是我的意思,抱歉。好的,工作得很好!你从哪里知道的?好的,很好用!你从哪里知道的?