C++ – Issue Solved by New ‘using’ Syntax for Template Typedefs

c++c++11template-aliasestemplatestypedef

In C++11 you can create a "type alias" by doing something like

template <typename T>
using stringpair = std::pair<std::string, T>;

But this is a deviation from what you'd expect a template typedef would look like:

template <typename T>
typedef std::pair<std::string, T> stringpair;

So this raises the question – why did they need to come up with a new syntax? what was it that did not work with the old typedef syntax?

I realize the last bit doesn't compile but why can't it be made to compile?

Best Answer

I'll just refer to stroustrup himself:

http://www.stroustrup.com/C++11FAQ.html#template-alias

The keyword using is used to get a linear notation "name followed by what it refers to." We tried with the conventional and convoluted typedef solution, but never managed to get a complete and coherent solution until we settled on a less obscure syntax.

Related Question