views:

102

answers:

2

Hi, 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.

+8  A: 

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
Note that this may require the use of `typename` keyword if `B` or `C` are themselves template parameters.
Matthieu M.
+5  A: 

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