Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a type alias for a template in a class [duplicate]

Tags:

For example

struct Option_1
{
    template<class T> using Vector = std::vector<T>;
};

I can do

typename Option_1::Vector<int> v;

But I prefer the following

Vector<Option_1, int> v;

or similars without the word "typename". I define an alias

template<class Option, class T> using Vector= typename Option::Vector<T>;

but failed with unrecognizable template declaration/definition. How to fix it?

like image 273
user1899020 Avatar asked Mar 03 '17 16:03

user1899020


People also ask

What is template type alias?

Type alias is a name that refers to a previously defined type (similar to typedef). Alias template is a name that refers to a family of types.

What is an alias in C++?

You can use an alias declaration to declare a name to use as a synonym for a previously declared type. (This mechanism is also referred to informally as a type alias). You can also use this mechanism to create an alias template, which can be useful for custom allocators.

How do you declare a typename in C++?

You must add the keyword typename to the beginning of this declaration: typename A::C d; You can also use the keyword typename in place of the keyword class in template parameter declarations.

Can template class inherit?

Inheriting from a template classIt is possible to inherit from a template class. All the usual rules for inheritance and polymorphism apply. If we want the new, derived class to be generic it should also be a template class; and pass its template parameter along to the base class.


1 Answers

You should use the keyword template for the dependent template name Option::Vector, i.e.

template<class Option, class T> using Vector = typename Option::template Vector<T>;
//                                                              ~~~~~~~~

LIVE

like image 169
songyuanyao Avatar answered Sep 21 '22 10:09

songyuanyao