简体   繁体   English

部分模板的C ++ typedef

[英]C++ typedef for partial templates

i need to do a typedef like this. 我需要做一个像这样的typedef。

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++. 我刚刚发现它在C ++中不受支持。 Can someone advise me on how to achieve the same through alternative means? 有人可以告诉我如何通过替代手段实现同样的目标吗?

Thanks, Gokul. 谢谢,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): 如果您有一个C ++ 0x / C ++ 1x编译器,那么将允许使用略有不同的语法(似乎编译器仍然不支持此功能):

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): 您可以使用其他技术,例如在模板化结构中定义封闭类型(如Pieter建议),或滥用继承(尽可能避免):

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 ). 这个想法称为模板别名 ,是C ++ 0x标准( 提案 )的一部分。 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. 并使用Y<B, C>::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 :). 而你可能会倾向于认为gcc4.5或VS2010可能已经支持它,就像C ++ 0x的一个重要子集的情况一样,但我不得不让你失望,因为我们说话仍然不支持:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM