使用类型 = SMTH.和模板化类型

using type = smth. and templated types

本文关键字:类型 SMTH      更新时间:2023-10-16

我在一段C++代码上遇到了一些麻烦。

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

struct A
{
    using my_type1 = double;
    using my_type2 = int;
};
struct B
{
    using size_type = A::my_type2;
};

但是,我希望能够选择my_type1,所以我选择了模板方式:

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;
};

在这里,gcc 失败,行上有:"预期的类型说明符"

using size_type = A<T>::my_type2;

我也可以my_type2放入模板中,但这是一种不应有太大变化的类型。

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

您必须添加typename

using size_type = typename A<T>::my_type2;