开发者

C++ typedef for partial templates

开发者 https://www.devze.com 2023-01-02 21:37 出处:网络
i need to do a typedef like this. template< class A, class B, class C > class X { }; template< class B, class C >

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 achie开发者_高级运维ve the same through alternative means?

Thanks, Gokul.


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


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 :).

0

精彩评论

暂无评论...
验证码 换一张
取 消