Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between using and using = for types?

Tags:

c++

using

Consider the following two using statements:

using ::space1::space2::MyType;
using MyType = ::space1::space2::MyType;

It seems that after the using, both ways allows us the use MyType directly (without any qualifiers).

So what's the difference between the above two?

like image 937
OneZero Avatar asked Sep 27 '22 07:09

OneZero


1 Answers

As said in the comment, in the first case you are actually exporting a name from a namespace to the one which contains the using declaration, while in the second case you are defining an alias in the namespace that contains the using statement that points to that specific name and its surrounding namespace.

As an example, with the second expression you can define aliases like the following one:

using Foo = Bar<MyClass>;

template <class C>
using Foo = Bar<C, MyClass>;

While the first using expression cannot, it serves merely to let names to be available in different spaces than the ones that contain them.

See here for further details regarding using directives, using declarations and aliases (type and template ones).

like image 95
skypjack Avatar answered Sep 30 '22 07:09

skypjack