Tuesday, February 5, 2019

c++ - What's the difference between using-style and typedef-style?




using IntegerType1 = int;
typedef int IntegerType2;


int main()
{
IntegerType1 n1 = 1; // OK
IntegerType2 n2 = 2; // OK
}


My questions are:





  1. What's the difference between using-style and typedef-style?


  2. As we already have typedef-style, what's the motivation to make using-style become a C++ standard?



Answer



The "using-style" was introduced to allow templated typedefs:



template< typename T >
using int_map = std::map< int, T >;



You can not do this with typedef. I found it strange myself that it was decided to use using and not typedef as the keyword for this, but I guess the committee must have found some problem with extending the typedef syntax.


No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...