Typedef of template alias in C ++
I have the following templated class A
:
template<template<typename>class VectorT>
class A
{
//...
}
which I create 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 with a name Vector
inside A
:
template<template<typename>class VectorT>
class A
{
public:
template<typename T>
using Vector = VectorT<T>;
//...
}
so inside A
I can call Vector<int>
(instead of VectorT<int>
). More importantly, 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>
}
to create an attribute Vector<int>
on a class B
eg. So I tried 3 things (inside a 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 typename A::Vector
is naming StdVector
which is not a type (I'm assuming it is only considered an alias and not a type?) For the first two solutions. And the last solution creates a syntax error.
Here is all the 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 patterns ...
source to share