2
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?

xmllmx
  • 39,765
  • 26
  • 162
  • 323

2 Answers2

5

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.

xmllmx
  • 39,765
  • 26
  • 162
  • 323
Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
  • 5
    With `using` the name of the newly defined type is always on the left side of expression. With `typedef` the name can be in the middle of expression. So `using` improves readability, IMHO. – Vasily Biryukov Sep 14 '13 at 07:53
  • @VasilyBiryukov True and I personally like the new `using`-syntax much better than the old `typedef`-syntax. It also has other benefits when you need to apply alignment with [`alignas`](http://stackoverflow.com/a/15912208/2073257). – Daniel Frey Sep 14 '13 at 07:56
3

I find that readability is greatly improved even for non-templates:

typedef void (*FunctionPtr)();  // right-to-left, identifier in the middle of the definition
using FunctionPtr = void (*)(); // left-to-right, same as variables

It's probably minor, but in template-metaprogramming this syntactic advantage makes a program easier to read, and makes template metafunctions easier to refactor towards constexpr functions. Essentially replace

using T = type_expression;
constexpr auto v = value_expression;

Furthermore (appeal to authority), it's also in the draft Effective C++11/14 guidelines.

TemplateRex
  • 69,038
  • 19
  • 164
  • 304