Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What was the issue solved by the new "using" syntax for template typedefs?

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?

like image 625
shoosh Avatar asked Oct 17 '13 22:10

shoosh


People also ask

Are Typedefs scoped?

A typedef is scoped exactly as the object declaration would have been, so it can be file scoped or local to a block or (in C++) to a namespace or class.

What is the function of typedef in C++?

The typedef keyword allows the programmer to create new names for types such as int or, more commonly in C++, templated types--it literally stands for "type definition". Typedefs can be used both to provide more clarity to your code and to make it easier to make changes to the underlying data types that you use.

Does typedef create a new type?

Syntax. Note that a typedef declaration does not create types. It creates synonyms for existing types, or names for types that could be specified in other ways.

What is typedef declaration?

The typedef declaration provides a way to declare an identifier as a type alias, to be used to replace a possibly complex type name.


1 Answers

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.

like image 192
dornhege Avatar answered Oct 23 '22 04:10

dornhege