1.支持花括號初始化
void test1()
{vector<string> v1 = { "asd","asd","add" };vector<string> v2{ "asd","asd","add" };map<string, int> m1={ {"asd",1},{"asd",2},{"asd",3} };map<string, int> m2{ {"asd",1},{"asd",2},{"asd",3} };}
當編譯器遇到花括號初始化表達式時,會發生以下的步驟:
template<class T>
class F
{
public:F(std::initializer_list<T> il) {}; // 如果該構造函數存在直接就調用F() {};
};
步驟 1:
優先匹配 std::initializer_list 構造函數 如果類型 F定義了 接受 std::initializer_list的構造函數 (如 F(std::initializer_list<U>)),編譯器會優先嘗試調用它。 此時,花括號內的所有參數會被打包成一個 std::initializer_list<T>臨時對象,其中 T 是花括號內元素的統一類型(需支持隱式轉換)。
步驟2:
?如果 initializer_list構造函數不匹配(如參數類型無法轉換,或未定義該構造函數),編譯器會按常規構造函數重載決議規則選擇其他構造函數。 花括號內的參數會逐個傳遞給構造函數,如同圓括號初始化。
步驟3:
聚合類型的直接初始化 如果T是聚合類型(如沒有用戶自定義構造函數的結構體、數組等),花括號會按聲明順序直接初始化成員。
template<class T>
class F
{
public://F(std::initializer_list<T> il) {}; // 如果該構造函數存在直接就調用//F() {};int _a;double _b;
};void test()
{F<int> f{ 1,1.0 }; // 按聲明順序初始化
}
注意:
1.窄化轉換的檢查 無論調用哪種構造函數,花括號初始化都會靜態檢查窄化轉換(如 double→int),若存在則編譯失敗。
template<class T>
class F
{
public://F(std::initializer_list<T> il) {}; // 如果該構造函數存在直接就調用//F() {};int _a;double _b;
};void test()
{F<int> f{ 1.1,1.0 }; // 按聲明順序初始化 // double->int 本應該發生窄化報錯,但發生了自定義類型的隱式類型的轉化int x{ 1.5 }; // 編譯時報錯
}
2.當默認構造函數和initlalizer_list都存在時,花括號里面非空,調用initializer_list,空就調默認構造函數。
3.模板函數需要顯示參數類型。
template<typename T>
void foo(T param) {}void test0()
{//foo({ 1, 2 }); // 錯誤:無法推導 T(必須顯式指定為 std::initializer_list<int>)foo(initializer_list<int>{1, 2});
}
2.decltype自動推導類型
void test2()
{int a = 1;double b = 2.0;auto c = a + b; // auto不能作形參和返回值cout << typeid(c).name() << endl; // doublestring s;decltype(c) d;cout << typeid(d).name() << endl; // doubledecltype(s) e;cout << typeid(e).name() << endl; //class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >
}
3.final 和override
?final 修飾類,類就變成了最終類,不能被繼承,修飾虛函數,該虛函數不能被重寫。? ? ? ? ? ? override 檢查子類是否重寫虛函數。
4.?顯示調用默認構造函數
class A
{
public:A() = default; //指定顯示去生成默認構造函數,因為當自己寫了構造函數,就不會默認生成,但如果傳參不匹配,無法構造,又不能調用默認構造函數,會報錯,所以顯示調用。A(const int& a):_a(a){ }private:int _a = 20;
};void test3()
{A a; // 當沒有A() = default,會報錯
}
5.禁止拷貝
class B
{
private:// B(const B& b); // C++98 只聲明,不實現,別人就無法拷貝對象,為了防止在類外定義,用private限定// 在C++11中定義了新的語法禁止拷貝B(const B& b)= delete;B& operator=(const B& b) = delete;
};
6.右值引用
void test5()
{// 左值通常都是變量// 右值通常都是常量(表達式或者函數返回的值)int a = 0;int& b = a; // 左值引用 引用左值// 如果用 左值引用 引用右值要加constconst int& c = 0;// 右值引用 引用右值int&& d = 1; // 節省空間int e = 1; // 直接賦值和右值引用的區別,右值引用不需要開辟空間,拷貝對象// 右值引用 引用左值要加movecout << "e的類型:" << typeid(e).name() << endl;int&& f = move(e); // f與e的地址一樣。cout << "e的類型:" << typeid(e).name() << endl;auto f1 = move(e);e = 3;cout <<"e:"<< e << " " <<"f:"<< f << endl;f = 2;cout <<"e:"<<e << " " <<"f:"<< f << endl;// 右值引用的是常量,為什么f能改變呢?并且將e改變了int&& g = 1 + 2;int& h = g; // 雖然不能對常量用& 但可以對g用&cout << "&h:" << &h << endl;cout << "&g:" << &g << endl;h = 5;cout << "g:" << g << " " << "h:" << h << endl;// 可以通過h改變g}
move對對像本身沒什么影響,將對象標記為可以移動的,可以調用vector或string的移動構造函數,但移動構造函數將原來對象對這片內存的權限取消了,給了新的對象,將原來的對象置空或0。
其實,右值引用的作用主要發揮在調用移動構造,只轉移管理權,不開辟空間,減少了內存的開銷。(前提是,如果要調用移動構造,move的對象你不再使用了),對于一般的,如上面的move的使用,與左值引用無異。
7.lambda表達式
格式:?[捕捉列表](形參)->返回類型{函數體}
void test8()
{auto r3 = [](int x) {return x; }; // r3 is a name of lambdacout << typeid(r3).name() << endl;int a = 1;int b = 2;int y = 3;int x = 0;cout << "a:" << a << " " << "b:" << b << endl;auto r1=[&a, &b](int x, int y)->int{a++; b++; return x + y; }; // [捕捉列表](形參)->返回類型{函數體}int ret=r1(x, y);cout << "a:" << a << " " << "b:" << b << " " << "ret:" << ret << endl;// 不作列表只能捕捉同一作用域變量//傳值捕捉的對象不能改變(值的大小)//auto rr = [a, b]() {// int tmp = a;// a = b;// b = tmp;// } // 報錯auto rr = [a, b]()mutable {int tmp = a; // (改變了值的大小)a = b;b = tmp; // 加mutable就可以了,但沒意義,用引用捕獲就行};// 用途vector<int> v{ 1,2,3,4,5,6,7,8,9,10 };int num = count_if(v.begin(), v.end(), [](int n) {return n % 3==0; }); // num 大小為返回true的個數 // count_if 返回滿足條件的范圍中的元素數cout <<"滿足條件個數:"<< num << endl;// for_each 將函數應用于范圍for_each(v.begin(), v.end(), [](int& v) {cout << v << " "; });}結果:
class `void __cdecl test8(void)'::`2'::<lambda_1>
a:1 b:2
a:2 b:3 ret:3
滿足條件個數:3
1 2 3 4 5 6 7 8 9 10
《C++ primer》解釋
8.引用折疊與完美轉發
核心規則: 只要有一個 &(左值引用),最終結果就是 T&(左值引用)。 只有全是 &&右值引用),最終結果才是 T&&(右值引用)。
template<class T>
void B1(T&& t) { cout << "void B(T&& t)" << endl; };
template<class T>
void B1(T& t) { cout << "void B(T& t)" << endl; };
template<class T>
void B1(const T&& t) { cout << "void B(const T&& t)" << endl; };
template<class T>
void B1(const T& t) { cout << "void B(const T& t)" << endl; };template<class T>
void A1(T&& t) //第一次引用折疊
{//右值引用在第二次傳參后,屬性丟失//完美轉發解決B1(t);//B1(std::forward<T>(t)); // 調用forward 發生第二次引用折疊
}void test9()
{int a = 1;const int b = 2;A1(a); A1(b); A1(move(a)); A1(move(b)); // 只有左值引用會引發引用折疊// int&& c = move(b); // 不允許直接move有const修飾的對象const int&& c = move(b);// int&& c = move(const_cast<int>(b)); // 用const_cast,是const修飾的變量指向的數據是可以修改的,2是一個常量}結果:
void B(T& t)
void B(const T& t)
void B(T& t)
void B(const T& t)
可以看出在第二次傳參時,值的屬性消失
B1(std::forward<T>(t)); // 調用forward 發生引用折疊結果:
void B(T& t)
void B(const T& t)
void B(T&& t)
void B(const T&& t)屬性沒有丟失
根據折疊規則:
forward函數:
emplate<typename T>
T&& forward(std::remove_reference_t<T>& t) noexcept {return static_cast<T&&>(t); // 引用折疊發生在這里
}
根據折疊規則:
如果 T是 int&: static_cast<int& &&>(t) → static_cast<int&>(t)(保持左值)。
如果 T是 int: static_cast<int&&>(t)(保持右值)。
forward很巧妙的使用了折疊的規則:
如果沒有forward折疊,那么下面兩種 int ,const int會被推導為int&,const int&
因:第一次折疊搗的鬼。
果:forward巧妙的借用了折疊鬼完成完美轉發。
師夷長技以制夷。
在 A1(move(a)) 的場景中,雖然 A1()函數中的t 的類型已經是 int&&(右值引用),直接傳遞 t 似乎也能保留右值屬性,但使用 std::forward 仍然必要
?直接傳遞 t 的問題 :右值引用變量 t是左值表達式,在 A1函數內部,t 是一個具名變量(即使它的類型是 int&&),而所有具名變量都是左值表達式。?
9.線程
int x = 0;
void Add2(int num)
{for (int i = 0; i < num; i++){++x;}
}void test10()
{thread t1(Add2, 1000000);thread t2(Add2, 1000000);t1.join();t2.join(); // t1和t1 同時進行線程,對x加10000000次,會有沖突cout << "x:" << x << endl; // x<2000000}
不同的線程對同一個對象進行處理,會發生同時拿這個對象,同時加1,x只加一次結果(多次):
x:1044214
x:1052133
x:1187482
9.1鎖的使用
mutex mtx; // 不能將互斥鎖放在Add3()里,不然每個線程創建自己的互斥鎖
void Add3(int num)
{mtx.lock();for (int i = 0; i < num; i++){++m;}mtx.unlock();
} // 串行void test10()
{// 互斥鎖thread t3(Add3, 1000000);thread t4(Add3, 1000000);t3.join();t4.join(); cout << "m:" << m << endl; }結果:
m:2000000
9.2原子
void test11()
{atomic<int> x2 = 0;auto Add5 = [&x2](int num) {for (int i = 0; i < num; i++){++x2;}};thread t1(Add5, 1000000);thread t2(Add5, 1000000);cout << t1.get_id() << endl;cout << t2.get_id() << endl;t1.join();t2.join(); cout << "x:" << x2 << endl; // 原子不上鎖,也準確。}
原子不會有沖突。
9.3條件變量
成員
雙線程打印1~100,一個線程打印奇數,另一個線程打印偶數
void test13()
{int num = 100;condition_variable cv1;condition_variable cv2;mutex m1;mutex m2;thread t1([&]() {for (int i = 0; i < num; i += 2){if (i) // 第一次不鎖{unique_lock<mutex>lock1(m1);cv1.wait(lock1);}cout << this_thread::get_id() << ":" << i << endl;cv2.notify_one();}});thread t2([&]{for (int i = 1; i < num; i += 2){unique_lock<mutex>lock2(m2);cv2.wait(lock2);cout << this_thread::get_id() << ":" << i << endl;cv1.notify_one();}});t1.join();t2.join();}