What's the difference between using

This question already has an answer here:

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

  • 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.


    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.

    链接地址: http://www.djcxy.com/p/78642.html

    上一篇: 将嵌套类导入名称空间

    下一篇: 使用有什么区别