詳解C++14中返回類型推導(dǎo)的使用
使用C++14中的auto返回類型,編譯器將嘗試自動(dòng)推導(dǎo)(deduce)返回類型:
namespace { int xx = 1; auto f() { return xx; } // return type is int const auto& f3() { return xx; } // return type is const int& auto multiply(int a, int b) { return (a * b); } auto& increase(int& a) { a++; return a; } } // namespace int test_auto_14_1() { int a = 4, b = 5; auto v1 = multiply(a, b); std::cout << "v1:" << v1 << "\n"; // v1:20 auto& v2 = increase(a); std::cout << "v2:" << v2 << "\n"; // v2:5 v2 = 10; std::cout << "a:" << a << "\n"; // a:10 return 0; }
在C++14中使用lambda,可以使用auto推導(dǎo)其返回類型,這使得返回推導(dǎo)引用或右值引用成為可能:
namespace { template <typename T> auto& f2(T& t) { return t; } } // namespace int test_auto_14_2() { // returns a reference to a deduced type auto g = [](auto& x) -> auto& { return f2(x); }; int y = 123; int& z = g(y); // reference to y std::cout << "z:" << z << "\n"; // z:123 z = 456; std::cout << "y:" << y << "\n"; // y:456 return 0; }
decltype(auto)類型說明符(type-specifier)也像auto一樣推導(dǎo)出一個(gè)類型。但是,它會(huì)在保留其引用和cv限定符(cv-qualifier)的同時(shí)推導(dǎo)返回類型,而auto則不會(huì):
namespace { decltype(auto) increase(int& a) { a++; return a; } int xxx = 123; auto f(const int& i) { return i; } // return type is "int" static_assert(std::is_same<const int&, decltype(f(xxx))>::value == 0); static_assert(std::is_same<int, decltype(f(xxx))>::value == 1); decltype(auto) g(const int& i) { return i; } // return type is "const int&" static_assert(std::is_same<const int&, decltype(g(xxx))>::value == 1); int xx = 1; decltype(auto) f2() { return xx; } // return type is int, same as decltype(x) static_assert(std::is_same<int, decltype(f2())>::value == 1); decltype(auto) f3() { return(xx); } // return type is int&, same as decltype((x)) static_assert(std::is_same<int&, decltype(f3())>::value == 1); //const decltype(auto)& f4(const int) { return xx; } // "const decltype(auto)&" is an error, decltype(auto) must be used on its own // error:“decltype(auto)”不能與任何其他類型說明符組合 //auto f5(bool flag) { // if (flag) return 1; // else return 1.0; // error: 所有返回表達(dá)式必須推導(dǎo)為相同類型: 以前為“int” //} //class AA { // virtual auto g() { return 1; } // error: 虛成員函數(shù)不應(yīng)具有含“auto”的返回類型 //}; } // namespace int test_decltype_14_1() { int a = 4; auto& v2 = increase(a); std::cout << "v2:" << v2 << "\n"; // v2:5 v2 = 10; std::cout << "a:" << a << "\n"; // a:10 return 0; } int test_decltype_14_2() { const int x = 0; //x = 2; // error C3892: “x”: 不能給常量賦值 auto x1 = x; // int x1 = 2; decltype(auto) x2 = x; // const int //x2 = 3; // error C3892: “x2”: 不能給常量賦值 int y = 2; int& y1 = y; auto y2 = y1; // int y2 = 5; std::cout << "y:" << y << "\n"; // y:2 decltype(auto) y3 = y1; // int& y3 = 10; std::cout << "y:" << y << "\n"; // y:10 int&& z = 2; auto z1 = std::move(z); // int z1 = 5; std::cout << "z:" << z << "\n"; // z:2 decltype(auto) z2 = std::move(z); // int&& z2 = 10; std::cout << "z:" << z << "\n"; // z:10 return 0; }
windows下debug模式結(jié)果如下:
注:
(1).const decltype(auto)&是一個(gè)error,decltype(auto)必須單獨(dú)使用,它不能與任何其它類型說明符組合。
(2).函數(shù)返回類型為auto或decltype(auto):如果有多個(gè)return語句,它們必須全部推導(dǎo)出相同的類型,否則會(huì)編譯error。
(3).虛函數(shù)不能使用返回類型推導(dǎo)。
C++11中auto的使用:一文詳解C++11中auto的使用
C+11中decltype的使用:一文詳解C++11中decltype的使用
GitHub:https://github.com/fengbingchun/Messy_Test
到此這篇關(guān)于詳解C++14中返回類型推導(dǎo)的使用的文章就介紹到這了,更多相關(guān)C++14返回類型推導(dǎo)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C/C++使用API實(shí)現(xiàn)數(shù)據(jù)的壓縮與解壓縮
在Windows編程中,經(jīng)常會(huì)遇到需要對(duì)數(shù)據(jù)進(jìn)行壓縮和解壓縮的情況,本文將深入探討使用Windows API進(jìn)行數(shù)據(jù)壓縮與解壓縮的過程,感興趣的小伙伴可以了解下2023-11-11gazebo里通過節(jié)點(diǎn)發(fā)布topic讓關(guān)節(jié)轉(zhuǎn)動(dòng)實(shí)現(xiàn)詳解
這篇文章主要介紹了gazebo里通過節(jié)點(diǎn)發(fā)布topic讓關(guān)節(jié)轉(zhuǎn)動(dòng)實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12C語言根據(jù)協(xié)議分割獲取字符串單元的實(shí)現(xiàn)代碼
今天小編就為大家分享一篇關(guān)于C語言根據(jù)協(xié)議分割獲取字符串單元的實(shí)現(xiàn)代碼,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12C++11之std::future對(duì)象的使用以及說明
這篇文章主要介紹了C++11之std::future對(duì)象的使用以及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02C語言內(nèi)存的動(dòng)態(tài)分配比較malloc和realloc的區(qū)別
這篇文章主要介紹了C語言內(nèi)存的動(dòng)態(tài)分配比較malloc和realloc的區(qū)別,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是本文的詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07