19

i need to do a typedef like this.

template< class A, class B, class C >
class X
{
};

template< class B, class C >
typedef X< std::vector<B>, B, C >  Y;

I just found that it is not supported in C++. Can someone advise me on how to achieve the same through alternative means?

Thanks, Gokul.

Gokul
  • 893
  • 11
  • 27

2 Answers2

22

If you have a C++0x/C++1x compiler, that will be allowed with a slightly different syntax (it seems as if compilers don't still support this feature):

template <typename B, typename C>
using Y = X< std::vector<B>, B, C >;

You can use other techniques, like defining an enclosed type in a templated struct (like Pieter suggests), or abusing inheritance (avoid if possible):

template <typename B, typename C>
class Y : public X< std::vector<B>, B, C > {};
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • 1
    It makes me wonder though *why* it is not yet supported. Is it so much harder than move semantics, `auto` typing, variadic templates or some other stuff that at least seems difficult to implement? But I don't have much experience in building a compiler, my intuition on how hard stuff is could be *way* off :). – Pieter Jun 08 '10 at 11:55
  • 2
    I think different parts of the standard were decided on at different times - compiler makers have to be reasonably confident a feature won't change before the final standard comes out in 2011 before they spend time developing it - and then decide how that fits in to their release schedules. – AshleysBrain Jun 08 '10 at 12:38
  • 1
    I am a person of the future. It seems to be supported in major compilers now. – geometrian Sep 03 '14 at 01:54
  • I don't see how the second solution abuses inheritance. It exactly fits the idea of inheritance: deriving from one class to create another, more specialized class. Or in this case, deriving from one template to create a more specialized template. – orodbhen Jun 20 '15 at 04:27
  • 1
    At first thought, the idea of specializing a template using a typedef seems to make natural sense. But, in fact, it contradicts the idea of a typedef: a type definition. If the type isn't fully defined, then it's not a typedef. – orodbhen Jun 20 '15 at 04:42
  • @orodbhen: A `typedef` does not define a type, it did not in C and it does not in C++, it creates an alias so the rational in the last comment does not stand. The use of inheritance in the answer is really *abuse*. Inheritance is the feature of the language (of many languages) that is most often abused. The use of inheritance there does not create a type to be substitutable in place of the base, but a syntactic shortcut not to type. The problems are too many to put on a comment, but start with constructors (having to forward) and end with destructors (is `X` destructor virtual?) – David Rodríguez - dribeas Jun 21 '15 at 21:33
15

By placing it in a struct. This idea is called template alias and is part of the C++0x standard (the proposal). But a work-around is given by:

template<class B, class C>
struct Y {
  typedef X<std::vector<B>, B, C> type;
};

and use Y<B, C>::type as your desired type.

And you might be inclined to think gcc4.5 or VS2010 might already support it, as is the case with a big subset of C++0x, but I have to disappoint you, it is as we speak still unsupported :).

Pieter
  • 2,822
  • 18
  • 17