Item 9: Prefer Alias Declarations to typedefs

Because alias declarations may be templatized but typedefs can’t.

template<typename T>
using MyAllocList = std::list<T, MyAlloc<T>>;

MyAllocList<Foo> lw;

template<typename T>
class Foo {
  MyAllocList<T> list;
};

The equivalent code with typedef is:

template<typename T>
struct MyAllocList {
  typedef std::list<T, MyAlloc<T>> type;
};

MyAllocList<Foo>::type lw;

template<typename T>
class Foo {
  typename MyAllocList<T>::type list;
};

In C++14, type traits are migrated to alias declarations:

#include <type_traits>
std::remove_const<T>::type  // C++11
std::remove_const_t<T>      // C++14
Creative Commons License
This blog by Che-Liang Chiou is licensed under a Creative Commons Attribution 4.0 International License.