Saturday 2 December 2017

c++ - What is the difference between 'typedef' and 'using' in C++11?

itemprop="text">


I know that in C++11 we can
now use using to write type alias, like
typedefs:



typedef
int MyInt;


Is, from
what I understand, equivalent
to:



using MyInt =
int;



And
that new syntax emerged from the effort to have a way to express "template
typedef
":



template<
class T > using MyType = AnotherType< T, MyAllocatorType
>;


But, with the
first two non-template examples, are there any other subtle differences in the standard?
For example, typedefs do aliasing in a "weak" way. That is it
does not create a new type but only a new name (conversions are implicit between those
names).



Is it the same with
using or does it generate a new type? Are there any
differences?


itemprop="text">
class="normal">Answer





They are equivalent, from the
standard (emphasis mine)
(7.1.3.2):




A
typedef-name can also be introduced by an alias-declaration. The
identifier
following the using keyword becomes a typedef-name and the
optional
attribute-specifier-seq following the identifier appertains
to that
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.





No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...