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

来源:互联网 发布:广数980g76编程实例 编辑:程序博客网 时间:2024/06/05 15:39

出处: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 ignoreusing cell = pair<void*, cell*>; // ill-formed— end example ]
  • 定义模板的别名,只能使用using

使用外部构造

using A::A;

引入外部类型

using typename A; 

extern

外部模板

extern template<typename T>void(T t);
0 0