使用不与g ++一起使用的模板参数的typedef

时间:2013-12-12 18:51:23

标签: c++ c++11

以下代码在MVCC下编译但不是g ++,我不知道为什么。

template <typename T>
class A
{
public:
  template <typename C>
  class B
  {
  public:
    C* cptr;
  };

  typedef typename A<T>::B<T> Ttype;
};

int main(int argc,char** argv)
{
  A<int>::Ttype d;
  d.cptr = 0;
}

使用g ++,你得到

error: 'typename A<T>::B name template<class T> template<class C> class A<T>::B', which is not a type

我正在使用-std = c ++ 11

进行编译

3 个答案:

答案 0 :(得分:6)

我认为这是MVCC的一个错误。这不会编译,因为您需要使用template来消除B作为模板的歧义:

typedef typename A<T>::template B<T> Ttype;

答案 1 :(得分:6)

根据gcc错误消息,问题是您声称A<T>::B是一种类型,但它不是:它是一个类模板。 <{3}}和gcc都对

感到满意
typedef A<T>::B<T> Ttype;

即删除typename。在给定的上下文中,不可能将B专门化为与其显然不同的东西。

using - 别名的语法不同:

using Ttype = A<T>::B<T>;

使用额外template关键字的表示法首先声明B实际上是template,然后与实例化typename B<T>结合使用是一种类型:

typedef typename A<T>::template B<T> Ttype;

using Ttype = typename A<T>::template B<T>;

由于班级模板B无论如何都是本地的,因此在这种情况下并不需要这种资格,即

typedef B<T> Ttype;

using Ttype = B<T>;

也可以。

答案 2 :(得分:5)

对我来说看起来像个错误。

以下适用于GCC 4.8.1:

using Ttype = A<T>::B<T>;
相关问题