部分模板的C++ typedef

Gok*_*kul 17 c++ templates typedef

我需要做一个像这样的typedef.

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

template< class B, class C >
typedef X< std::vector<B>, B, C >  Y;
Run Code Online (Sandbox Code Playgroud)

我刚刚发现它在C++中不受支持.有人可以告诉我如何通过替代手段实现同样的目标吗?

谢谢,Gokul.

Dav*_*eas 20

如果您有一个C++ 0x/C++ 1x编译器,那么将允许使用略有不同的语法(似乎编译器仍然不支持此功能):

template <typename B, typename C>
using Y = X< std::vector<B>, B, C >;
Run Code Online (Sandbox Code Playgroud)

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

template <typename B, typename C>
class Y : public X< std::vector<B>, B, C > {};
Run Code Online (Sandbox Code Playgroud)

  • 我认为标准的不同部分是在不同的时间决定的 - 编译器制造商必须有理由相信,在最终标准出现之前,在2011年最终标准出现之前,功能不会发生变化,然后决定如何适应他们的发布时间表. (2认同)

Pie*_*ter 15

将它放在结构中.这个想法称为模板别名,是C++ 0x标准(提案)的一部分.但通过以下方式给出了解决方法:

template<class B, class C>
struct Y {
  typedef X<std::vector<B>, B, C> type;
};
Run Code Online (Sandbox Code Playgroud)

Y<B, C>::type用作您想要的类型.

而你可能会倾向于认为gcc4.5或VS2010可能已经支持它,就像C++ 0x的一个重要子集的情况一样,但我不得不让你失望,因为我们说话仍然不支持:).

  • 请注意,如果`B`或`C`本身是模板参数,则可能需要使用`typename`关键字. (2认同)