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

后端 未结 7 818
误落风尘
误落风尘 2020-11-22 03:32

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

typedef int MyInt;

Is, from what I

7条回答
  •  长发绾君心
    2020-11-22 04:09

    The using syntax has an advantage when used within templates. If you need the type abstraction, but also need to keep template parameter to be possible to be specified in future. You should write something like this.

    template  struct whatever {};
    
    template  struct rebind
    {
      typedef whatever type; // to make it possible to substitue the whatever in future.
    };
    
    rebind::type variable;
    
    template  struct bar { typename rebind::type _var_member; }
    

    But using syntax simplifies this use case.

    template  using my_type = whatever;
    
    my_type variable;
    template  struct baz { my_type _var_member; }
    

提交回复
热议问题