Typedef of a templated alias in C++
I have a the following template class A :
template<template<typename>class VectorT>
class A
{
//...
}
which I instantiate like this: A<MyStdVector> objectA; where MyStdVector is an alias for std::vector with a specific allocator ( MyAllocator ):
template<typename T>
using MyStdVector = std::vector<T,MyAllocator>
I decided to create an alias named Vector inside A :
template<template<typename>class VectorT>
class A
{
public:
template<typename T>
using Vector = VectorT<T>;
//...
}
such that inside A I can call Vector<int> (instead of VectorT<int> ). More important, I would like to access this alias Vector from another class B . How to achieve this :
template<class A>
class B
{
public:
// How to define a type Vector which refers to A::Vector
// such that inside B, Vector<int> refers to A::Vector<int>
// which refers to MyStdVector<int>
}
in order to create an attribute Vector<int> in the class B for instance. So I have tried 3 things (inside class B ):
typedef typename A::Vector Vector; //1
template<typename T>
using Vector = typename A::Vector; //2
template<typename T>
using Vector = typename A::Vector<T> //3
But the compiler says that the typename A::Vector names StdVector which is not a type (I guess it is only considered as an alias and not a type?) for the 2 first solutions. And the last solution produces a syntax error.
Here is the whole code I tried to compile :
#include <vector>
template<typename T>
using MyStdVector = std::vector<T/*,MyAllocator*/>;
template<template<typename>class VectorT>
class A
{
public:
template<typename T>
using Vector = VectorT<T>;
//...
};
template<class A>
class B
{
public:
// typedef typename A::Vector Vector; // 1
// template<typename T>
// using Vector = typename A::Vector; // 2
// template<typename T>
// using Vector = typename A::Vector<T>; // 3
Vector<int> m_vector;
};
int main(int argc, char *argv[])
{
A<MyStdVector> a;
B<A<MyStdVector>> b;
return 0;
}
I am confused about the difference between typedef and alias , especially when I want to mix them and they are templated...
键入3添加template
template <typename T>
using Vector = typename A::template Vector<T>;
链接地址: http://www.djcxy.com/p/82544.html
上一篇: 别名模板参数包
