如何在 typedef 中使用模板化类的数据成员

How to use data member of templatized class in typedef

本文关键字:数据成员 typedef      更新时间:2023-10-16
template <class T>
struct A {  
    typedef B type;
}

template<>
struct A<double> 
{
    typedef double type;
};

template<typename T, typename U>
B<U> func()
{
   A<U>::type my_type;
   my_type tmp;
}

此代码不使用 g++ 编译器进行编译。 错误消息是:

错误:模板参数列表太少

有人可以解释一下。

谢谢和问候,范萨尔

首先,对有问题的错误表示歉意。 正确的问题应该是:

template <class T>
struct A {  
   typedef B type;
 }

template<>
struct A<double> 
{
   typedef double type;
};

template<typename T, typename U>
void func()
{
  typedef A<U>::type my_type;
   my_type tmp;
}

这里基本上的问题是编译器对语句感到困惑

typedef A<U>::type my_type;

无论是类的数据成员还是类型。 所以我们必须明确地说它是一种类型。所以用下面的语句替换上面的语句

typdedef typename A<U>::type my_type;