IdeaHat
IdeaHat

Reputation: 7881

typedef using template arguments not working with g++

The following code compiled under MVCC but not g++, and i'm not sure why.

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

with g++, you get

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

I am compiling with -std=c++11

Upvotes: 3

Views: 388

Answers (3)

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153830

Based on the gcc error message, the problem is you claim A<T>::B is a type but it isn't: it a class template. Both gcc and clang are happy with

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

i.e., remove the typename. In the given context it wouldn't be possible to specialize B to be something different than what it obviously is anyway.

The using-alias does just the same with a different syntax:

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

The notation using an extra template keyword first states that B is actually a template and then, in combination with the typename that the instantiation B<T> is a type:

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

or

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

Since the class template B is local anyway, the qualification isn't really needed in this context, i.e.

typedef B<T> Ttype;

and

using Ttype = B<T>;

do work as well.

Upvotes: 6

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

Looks like a bug to me.

The following works in GCC 4.8.1:

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

Upvotes: 5

David G
David G

Reputation: 96810

I think this is a bug with MVCC. This doesn't compile because you need to use template to disambiguate B as a template:

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

Upvotes: 6

Related Questions