Monday, 18 September 2017

Difference between typedef and C++11 type alias




I'm reading template aliases here: http://en.cppreference.com/w/cpp/language/type_alias



And I'm wondering, even if it's written on the very first line of the page I linked, what's the difference between a typedef and a type alias (using mytype = T;)




Aren't they interchangeable?


Answer



There is absolutely no difference between both.



If you take a look at the standard :




7.1.3 The typedef specifier [dcl.typedef ]




A typedef-name can also be introduced by an alias-declaration. The identifier following the using keyword becomes a typedef-name. It has the same semantics as if it were introduced by the typedef specifier. In particular, it does not define a new type and it shall not appear in the type-id.



7.3.3 The using declaration [namespace.udecl]



If a using-declaration uses the keyword typename and specifies a dependent name (14.6.2), the name introduced by the using-declaration is treated as a typedef-name.







However from this page : http://en.cppreference.com/w/cpp/language/type_alias




It is said :




Type aliases are similar to typedefs, however, have the advantage of working with templates.




It seems that this



// template type alias

template using ptr = T*;
// the name 'ptr' is now an alias for pointer to T
ptr x;


is only possible with the using directive.






And do not forget that this is a C++11 feature. Some compilers do not support it yet.



No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...