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)
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的一个重要子集的情况一样,但我不得不让你失望,因为我们说话仍然不支持:).