1. 程式人生 > >C++11新特性總結

C++11新特性總結

constexpr 泛化的常數表示式

extern template class 外部模板

std::vector v{ "xyzzy", "plugh", "abracadabra" } 統一的初始化

decltype(some_int) other;型別推導

auto my_onheap_lambda_func = new auto([=](int x) { /*...*/ });Lamda函式與表示式

void(*func_ptr)(int) = a_lambda_func;

templateauto adding_func(const Lhs &lhs, const Rhs &rhs) -> decltype

(lhs+rhs) {return lhs + rhs;}

auto func_name(int x, int y) -> int;

SomeType() : SomeType(42) {}物件構造的改進

virtual void some_func(int) override; // 病態的,不會重寫基類的方法顯示重寫 (覆蓋,override) final

struct Base1 final { }; struct Derived1 : Base1 { }; // 病態的, 因為類Base1被標記為final了 virtual void f() final;

enum class Enumeration強型別列舉

template <typename Second>模板的別名using TypedefName = SomeType<OtherType, Second, 5>;

typedef void (*FunctionType)(double);       // 老式語法using FunctionType = void (*)(double); // 新式語法

 U() {new(&p) Point();} // 由於Point的原因, 必須定義建構函式.  聯合union 的構造 無限制的unions

template<typename... Values> class tuple;可變引數模板

std::forward<Args>(params)…

u8"This is a Unicode Character: \u2018."//UTF-8 char新的字串字面值

u"This is a bigger Unicode Character: \u2018."//UTF-16 char16_t

U"This is a Unicode Character: \U00002018."//UTF-32 char32_t

R"(The String Data \ Stuff " )"

執行緒區域性儲存 新的執行緒區域性儲存的生存期(原有的靜態,動態,自動變數除外)由thread_local關鍵字指

NonCopyable() = default;明確預設和明確刪除的特殊成員函式  NonCopyable(const NonCopyable&) = delete;

void f(int) = delete;    // 不能呼叫這個函式

template<class T> void f(T) = delete;    //不能呼叫這個函式

static_assert(sizeof(int) <= sizeof(T), "T is not big enough!");//靜態斷言

sizeof(SomeType::member);//允許sizeof運算子作用在型別的資料成員

alignas(float) unsigned char c[sizeof(float)]//控制和查詢物件的對齊方式

set_new_handler允許實現垃圾回收

TR2  filesystem

std:thread::native_handle()執行緒支援

recursive_mutex  condition_variable_any  futures  promises   packaged_task async

typedef std::tuple <int, double, long &, const char *> test_tuple;// 元組

std::get<3>(proof) = " Beautiful!";  // 修改元組的第四個元素

<regex>正則表示式

unique_ptr 可以存放在 C++11 提出的那些需要移動語義的容器之中//通用智慧指標

std::uniform_int_distribution<int> distribution(0, 99); // 建立分佈,以離散均勻分佈方式在0到99之間產生隨機數 std::mt19937 engine; // 建立隨機數生成引擎 auto generator = std::bind(distribution, engine); // 利用 bind 隨機數生成引擎和分佈組合成一個隨機數生成器 int random = generator();  // 產生隨機數

封裝引用std::ref

std::is_integral<T1>::value && std::is_floating_point<T2>::value用於超程式設計的型別特徵

typename std::result_of<Obj(Arg)>::type用於計算函式物件返回型別的統一方法