C++ C+中模板化别名的Typedef+;
我有一个模板类C++ C+中模板化别名的Typedef+;,c++,c++11,templates,vector,alias,C++,C++11,Templates,Vector,Alias,我有一个模板类a: template<template<typename>class VectorT> class A { //... } template<template<typename>class VectorT> class A { public: template<typename T> using Vector = VectorT<T>; //... }
a
:
template<template<typename>class VectorT>
class A
{
//...
}
template<template<typename>class VectorT>
class A
{
public:
template<typename T>
using Vector = VectorT<T>;
//...
}
我决定在A
内部创建一个名为Vector
的别名:
template<template<typename>class VectorT>
class A
{
//...
}
template<template<typename>class VectorT>
class A
{
public:
template<typename T>
using Vector = VectorT<T>;
//...
}
例如,为了在类B
中创建属性Vector
。
因此,我尝试了三件事(在类内B
):
我对
typedef
和alias
之间的区别感到困惑,尤其是当我想将它们混合在一起,并且它们是模板化的…类型3添加一个模板
template <typename T>
using Vector = typename A::template Vector<T>;
模板
使用Vector=typename A::template Vector;
为了澄清您对typedef
(C++98)和别名声明(C++11)之间区别的困惑:区别在于模板
别名声明可以模板化,而typedef不能模板化。
使用using
使用用户定义的分配器将myvector声明为向量:
template<typename T>
using myvector = std::vector<T, myAllocator<T>> // alias declaration
myvector<int> myIntVector; // client code
::type
后缀很麻烦,使用编写的样板文件更少。这就是为什么您更喜欢别名声明而不是typedef
template <typename T>
using Vector = typename A::template Vector<T>;
template<typename T>
using myvector = std::vector<T, myAllocator<T>> // alias declaration
myvector<int> myIntVector; // client code
template<typename T>
struct myvector {
typedef std::vector<T, myAllocator<T>> type;
}
myvector<int>::type myIntVector; // client code