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?
From the WG21 proposal N1489 Template aliases (by Stroustrup and Dos Reis):
It has been suggested to (re)use the keyword
typedef
as done in the paper [4] to introduce template aliases:template<class T> typedef std::vector<T, MyAllocator<T> > Vec;
That notation has the advantage of using a keyword already known to introduce a type alias. However, it also displays several disavantages among which the confusion of using a keyword known to introduce an alias for a type-name in a context where the alias does not designate a type, but a template; Vec is not an alias for a type, and should not be taken for a typedef-name. The name Vec is a name for the family
std::vector<o, MyAllocator<o> >
where the bullet is a placeholder for a type-name. Consequently we do not propose the typedef syntax.On the other hand the sentence
template<class T> using Vec = std::vector<T, MyAllocator<T> >;
can be read/interpreted as: from now on, I'll be using
Vec<T>
as a synonym forstd::vector<T, MyAllocator<T> >
. With that reading, the new syntax for aliasing seems reasonably logical.
The paper [4] referred to in the above quote was a prior proposal WG21 N1406 Proposed Addition to C++: Typedef Templates (by Herb Sutter). It uses both a different syntax (typedef
vs using
) as well as a different nomenclature (typedef templates vs template aliases). Herb's proposed syntax didn't make it, but the nomenclature can sometimes be found in informal discussions.