C++11老关键字的新含义(auto, using,extern)

    xiaoxiao2021-03-25  112

    出处:http://blog.csdn.net/cnsword/article/details/8034947

    C++11对关键字进行了修订,加入了nullptr、constexpr、decltype、default、static_assert等,同时原有的关键字(auto,using,extern)含义和用途进行了修订。在这里主要了解一下对auto、using、extern这三个关键字的修订。

    auto

    自动化变量

    auto a = 12; auto b = 12.0f; auto c = true; auto d = [] (int x)->int{ return 12;}; auto e = std::bind(&func, _1);

    延迟绑定

    template<typename T, typename L> auto fun(T x, L y)->decltype(x + y){return x;}

    using

    定义别名

    template<typename T> using Tlist = std::list<T>; using Tlist = std::list<char>; using df = void(*)();//等价于typedef void(*df)() 和typedef的区别: 定义一般类型的别名没区别。C++11标准7.1.3.2:( isocpp.org/files/papers ) 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. [Example: using handler_t = void (*)(int); extern handler_t ignore; extern void (*ignore)(int); // redeclare ignore using cell = pair<void*, cell*>; // ill-formed — end example ] 定义模板的别名,只能使用using。

    使用外部构造

    using A::A;

    引入外部类型

    using typename A;

    extern

    外部模板

    extern template<typename T>void(T t);
    转载请注明原文地址: https://ju.6miu.com/read-21766.html

    最新回复(0)