2.3.1 Template Aliases

What was the problem

Before C++11, we could simply not declare a typedef with a parameterized type. For example, the following did not work:

// We cannot do that. 
template<typename T> 
typedef std::vector<T> collection; 
 
// We cannot do that either. 
template<typename V> 
typedef std::map<std::string, V> string_map;

The alternative was to use a struct to receive the type, then declare the typedef inside the struct:

// Workaround: nest the type. 
template<typename V> 
struct string_map 
{ 
  typedef std::map<std::string, V> type; 
}; 
 
// Easy to use, isn’t it? 
string_map<int>::type string_to_int;
How the Problem is Solved

Starting with C++11, the using keywork allows to declare templated type aliases:

// We can do that. 
template<typename T> 
using collection = std::vector<T>; 
 
// We can also do that. 
template<typename V> 
using string_map = std::map<std::string, V>; 
 
// Looks like a first-class type. 
string_map<int> string_to_int;

The using keyword is also a replacement for typedef in general.