问题描述
C ++无法从typedef或typedef模板类创建模板。
C++ is unable to make a template out of a typedef or typedef a templated class. I know if I inherit and make my class a template, it will work.
例如:
// Illegal
template <class T>
typedef MyVectorType vector<T>;
//Valid, but advantageous?
template <class T>
class MyVectorType : public vector<T> { };
这样做是有利的,所以我可以伪造typedef或有更好的方法?
Is doing this advantageous so that I can "fake" a typedef or are there better ways to do this?
推荐答案
C ++ 0x将添加模板typedef使用使用
C++0x will add template typedefs using the using
keyword.
您的解决方案声明了一个新类型,而不是类型别名,例如您不能使用向量< T>
初始化 MyVectorType&
这可能不是你的问题,但如果是,但是你不想引用你的代码中的矢量,你可以做:
Your solution declares a new type, not a type "alias", e.g. you cannot initialize a MyVectorType &
(reference) with a vector<T>
. This might not be a problem for you, but if it is, but you don't want to reference vector in your code, you can do:
template <typename T>
class MyVectorType {
public:
typedef std::vector<T> type;
};
这篇关于继承而不是typedef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!