自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C++14新特性的所有知識(shí)點(diǎn)全在這兒啦!

開(kāi)發(fā) 后端
這篇文章介紹下C++14新特性的所有知識(shí)點(diǎn)。讓我們一起來(lái)看看吧。

 [[342334]]    

前面程序喵介紹過(guò)C++11的新特性,這篇文章介紹下C++14的新特性。

「函數(shù)返回值類(lèi)型推導(dǎo)」

C++14對(duì)函數(shù)返回類(lèi)型推導(dǎo)規(guī)則做了優(yōu)化,先看一段代碼: 

  1. #include <iostream>  
  2. using namespace std;  
  3. auto func(int i) {  
  4.    return i;  
  5.  
  6. int main() {  
  7.    cout << func(4) << endl 
  8.    return 0;  

使用C++11編譯: 

  1. ~/test$ g++ test.cc -std=c++11  
  2. test.cc:5:16: error: ‘func’ function uses ‘auto’ type specifier without trailing return type  
  3. auto func(int i) {  
  4.                ^  
  5. test.cc:5:16: note: deduced return type only available with -std=c++14 or -std=gnu++14 

上面的代碼使用C++11是不能通過(guò)編譯的,通過(guò)編譯器輸出的信息也可以看見(jiàn)這個(gè)特性需要到C++14才被支持。

返回值類(lèi)型推導(dǎo)也可以用在模板中: 

  1. #include <iostream>  
  2. using namespace std;  
  3. template<typename T> auto func(T t) { return t; }  
  4. int main() { 
  5.    cout << func(4) << endl 
  6.    cout << func(3.4) << endl 
  7.    return 0;  

注意:

函數(shù)內(nèi)如果有多個(gè)return語(yǔ)句,它們必須返回相同的類(lèi)型,否則編譯失敗 

  1. auto func(bool flag) {  
  2.    if (flag) return 1;  
  3.    else return 2.3; // error  
  4.  
  5. // inconsistent deduction for auto return type: ‘int’ and then ‘double’ 

如果return語(yǔ)句返回初始化列表,返回值類(lèi)型推導(dǎo)也會(huì)失敗 

  1. auto func() {  
  2.    return {1, 2, 3}; // error returning initializer list  

如果函數(shù)是虛函數(shù),不能使用返回值類(lèi)型推導(dǎo) 

  1. struct A {  
  2. // error: virtual function cannot have deduced return type  
  3. virtual auto func() { return 1; }  

返回類(lèi)型推導(dǎo)可以用在前向聲明中,但是在使用它們之前,翻譯單元中必須能夠得到函數(shù)定義 

  1. auto f();               // declared, not yet defined  
  2. auto f() { return 42; } // defined, return type is int  
  3. int main() {  
  4. cout << f() << endl 

返回類(lèi)型推導(dǎo)可以用在遞歸函數(shù)中,但是遞歸調(diào)用必須以至少一個(gè)返回語(yǔ)句作為先導(dǎo),以便編譯器推導(dǎo)出返回類(lèi)型。 

  1. auto sum(int i) {  
  2.    if (i == 1)  
  3.        return i;              // return int  
  4.    else  
  5.        return sum(i - 1) + i; // ok  

lambda參數(shù)auto

在C++11中,lambda表達(dá)式參數(shù)需要使用具體的類(lèi)型聲明: 

  1. auto f = [] (int a) { return a; } 

在C++14中,對(duì)此進(jìn)行優(yōu)化,lambda表達(dá)式參數(shù)可以直接是auto: 

  1. auto f = [] (auto a) { return a; };  
  2. cout << f(1) << endl 
  3. cout << f(2.3f) << endl

變量模板

C++14支持變量模板: 

  1. template<class T>  
  2. constexpr T pi = T(3.1415926535897932385L);  
  3. int main() {  
  4.    cout << pi<int> << endl; // 3  
  5.    cout << pi<double> << endl; // 3.14159  
  6.    return 0;  

別名模板

C++14也支持別名模板: 

  1. template<typename T, typename U>  
  2. struct A {  
  3.    T t;  
  4.    U u;  
  5. };  
  6. template<typename T>  
  7. using B = A<T, int> 
  8. int main() {  
  9.    B<double> b;  
  10.    b.t = 10 
  11.    b.u = 20 
  12.    cout << b.t << endl 
  13.    cout << b.u << endl 
  14.    return 0;  

constexpr的限制

C++14相較于C++11對(duì)constexpr減少了一些限制:

C++11中constexpr函數(shù)可以使用遞歸,在C++14中可以使用局部變量和循環(huán) 

  1. constexpr int factorial(int n) { // C++14 和 C++11均可  
  2.    return n <= 1 ? 1 : (n * factorial(n - 1));  

在C++14中可以這樣做: 

  1. constexpr int factorial(int n) { // C++11中不可,C++14中可以  
  2.    int ret = 0 
  3.    for (int i = 0; i < n; ++i) {  
  4.        ret += i;  
  5.   }  
  6.    return ret;  

C++11中constexpr函數(shù)必須必須把所有東西都放在一個(gè)單獨(dú)的return語(yǔ)句中,而constexpr則無(wú)此限制 

  1. constexpr int func(bool flag) { // C++14 和 C++11均可  
  2.    return 0; 
  3.  

在C++14中可以這樣: 

  1. constexpr int func(bool flag) { // C++11中不可,C++14中可以  
  2.    if (flag) return 1;  
  3.    else return 0;  

[[deprecated]]標(biāo)記

C++14中增加了deprecated標(biāo)記,修飾類(lèi)、變、函數(shù)等,當(dāng)程序中使用到了被其修飾的代碼時(shí),編譯時(shí)被產(chǎn)生警告,用戶(hù)提示開(kāi)發(fā)者該標(biāo)記修飾的內(nèi)容將來(lái)可能會(huì)被丟棄,盡量不要使用。 

  1. struct [[deprecated]] A { };  
  2. int main() { 
  3.      A a;  
  4.     return 0;  

當(dāng)編譯時(shí),會(huì)出現(xiàn)如下警告: 

  1. ~/test$ g++ test.cc -std=c++14  
  2. test.cc: In function ‘int main()’:  
  3. test.cc:11:7: warning: ‘A’ is deprecated [-Wdeprecated-declarations]  
  4.      A a;  
  5.        ^  
  6. test.cc:6:23: note: declared here  
  7.  struct [[deprecated]] A { 

二進(jìn)制字面量與整形字面量分隔符

C++14引入了二進(jìn)制字面量,也引入了分隔符,防止看起來(lái)眼花哈~ 

  1. int a = 0b0001'0011'1010 
  2. double b = 3.14'1234'1234'1234; 

std::make_unique

我們都知道C++11中有std::make_shared,卻沒(méi)有std::make_unique,在C++14已經(jīng)改善。 

  1. struct A {};  
  2. std::unique_ptr<A> ptr = std::make_unique<A>(); 

std::shared_timed_mutex與std::shared_lock

C++14通過(guò)std::shared_timed_mutex和std::shared_lock來(lái)實(shí)現(xiàn)讀寫(xiě)鎖,保證多個(gè)線(xiàn)程可以同時(shí)讀,但是寫(xiě)線(xiàn)程必須獨(dú)立運(yùn)行,寫(xiě)操作不可以同時(shí)和讀操作一起進(jìn)行。

實(shí)現(xiàn)方式如下: 

  1. struct ThreadSafe {  
  2.     mutable std::shared_timed_mutex mutex_;  
  3.     int value_; 
  4.     ThreadSafe() {  
  5.         value_ = 0 
  6.     }  
  7.     int get() const {  
  8.         std::shared_lock<std::shared_timed_mutex> loc(mutex_);  
  9.         return value_;  
  10.     }  
  11.     void increase() { 
  12.          std::unique_lock<std::shared_timed_mutex> lock(mutex_);  
  13.         value_ += 1;  
  14.     }  
  15. }; 

為什么是timed的鎖呢,因?yàn)榭梢詭С瑫r(shí)時(shí)間,具體可以自行查詢(xún)相關(guān)資料哈,網(wǎng)上有很多。

std::integer_sequence 

  1. template<typename T, T... ints>  
  2. void print_sequence(std::integer_sequence<T, ints...> int_seq)  
  3.  
  4.     std::cout << "The sequence of size " << int_seq.size() << ": ";  
  5.     ((std::cout << ints << ' '), ...);  
  6.     std::cout << '\n';  
  7.  
  8. int main() {  
  9.     print_sequence(std::integer_sequence<int, 9, 2, 5, 1, 9, 1, 6>{});  
  10.     return 0;  
  11.  
  12. 輸出:7 9 2 5 1 9 1 6 

std::integer_sequence和std::tuple的配合使用: 

  1. template <std::size_t... Is, typename F, typename T>  
  2. auto map_filter_tuple(F f, T& t) {  
  3.     return std::make_tuple(f(std::get<Is>(t))...);  
  4. template <std::size_t... Is, typename F, typename T>  
  5. auto map_filter_tuple(std::index_sequence<Is...>, F f, T& t) {  
  6.     return std::make_tuple(f(std::get<Is>(t))...);  
  7.  
  8. template <typename S, typename F, typename T>  
  9. auto map_filter_tuple(F&& f, T& t) {  
  10.     return map_filter_tuple(S{}, std::forward<F>(f), t);  

std::exchange

直接看代碼吧: 

  1. int main() {  
  2.     std::vector<int> v;  
  3.     std::exchange(v, {1,2,3,4});  
  4.     cout << v.size() << endl 
  5.     for (int a : v) {  
  6.         cout << a << " ";  
  7.     }  
  8.     return 0;  

看樣子貌似和std::swap作用相同,那它倆有什么區(qū)別呢?

可以看下exchange的實(shí)現(xiàn): 

  1. template<class T, class U = T>  
  2. constexpr T exchange(T& obj, U&& new_value) {  
  3.     T old_value = std::move(obj);  
  4.     obj = std::forward<U>(new_value);  
  5.     return old_value;  

可以看見(jiàn)new_value的值給了obj,而沒(méi)有對(duì)new_value賦值,這里相信您已經(jīng)知道了它和swap的區(qū)別了吧!

std::quoted

C++14引入std::quoted用于給字符串添加雙引號(hào),直接看代碼: 

  1. int main() {  
  2.     string str = "hello world" 
  3.     cout << str << endl
  4.     cout << std::quoted(str) << endl 
  5.     return 0;  

編譯&輸出: 

  1. ~/test$ g++ test.cc -std=c++14  
  2. ~/test$ ./a.out  
  3. hello world  
  4. "hello world" 

關(guān)于C++14,我們今天先說(shuō)到這里。 

 

責(zé)任編輯:龐桂玉 來(lái)源: C語(yǔ)言與C++編程
相關(guān)推薦

2020-07-27 10:40:35

C++11語(yǔ)言代碼

2023-09-08 13:46:12

ArrayList數(shù)據(jù)存儲(chǔ)容器

2023-09-27 09:00:02

SpringBoot并發(fā)編程

2018-08-31 16:07:30

2020-10-22 12:30:33

MySQL

2024-01-24 11:59:44

Django自定義字段Python

2021-05-18 09:03:16

Gomapslice

2024-02-21 23:43:11

C++11C++開(kāi)發(fā)

2009-08-06 17:42:32

C#知識(shí)點(diǎn)

2009-08-05 09:22:43

C#調(diào)用VC DLL

2025-01-07 14:10:46

SpringBoot開(kāi)發(fā)Java

2024-02-02 18:00:11

C++代碼C++14

2019-08-15 09:35:03

2023-09-18 23:42:27

C++編程

2010-08-17 14:56:00

HCNE認(rèn)證

2011-04-15 12:25:21

BGP路由

2016-05-30 17:31:34

Spring框架

2024-11-25 11:00:00

模型訓(xùn)練

2021-04-13 08:25:12

測(cè)試開(kāi)發(fā)Java注解Spring
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)