1
我想MyVector可以选择std :: vector或boost :: container :: vector。如何实现它?我可以使用宏,但是我被告知它们不是很安全。谢谢。如何为类模板生成别名?
#define MyVector std::vector
// #define MyVector boost::container::vector
我想MyVector可以选择std :: vector或boost :: container :: vector。如何实现它?我可以使用宏,但是我被告知它们不是很安全。谢谢。如何为类模板生成别名?
#define MyVector std::vector
// #define MyVector boost::container::vector
C++ 11具有别名模板。你可以这样做:
template <typename T>
using MyVector = std::vector<T>;
//using MyVector = boost::container::vector<T>;
,然后用它像这样:
MyVector<int> x;
在C++ 03你要么使用宏或元函数。
template <typename T>
struct MyVector {
typedef std::vector<T> type;
//typedef boost::container::vector<T> type;
};
// usage is a bit tricky
MyVector<int>::type x;
// ... or when used in a template
typename MyVector<T>::type x;
嗯...我相信有一些尴尬与'type'成员typedeffing两种不同类型的:) – xtofl
@xtofl哎呀,忘了评论它。 –