最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解C++14中返回類型推導的使用

 更新時間:2023年07月02日 17:04:16   作者:fengbingchun  
這篇文章主要為大家詳細介紹了C++14中返回類型推導的使用,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以跟隨小編一起學習一下

使用C++14中的auto返回類型,編譯器將嘗試自動推導(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推導其返回類型,這使得返回推導引用或右值引用成為可能:

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一樣推導出一個類型。但是,它會在保留其引用和cv限定符(cv-qualifier)的同時推導返回類型,而auto則不會:

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: 所有返回表達式必須推導為相同類型: 以前為“int”
//}
//class AA {
//	virtual auto g() { return 1; } // error: 虛成員函數(shù)不應具有含“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模式結果如下:

注:

(1).const decltype(auto)&是一個error,decltype(auto)必須單獨使用,它不能與任何其它類型說明符組合。

(2).函數(shù)返回類型為auto或decltype(auto):如果有多個return語句,它們必須全部推導出相同的類型,否則會編譯error。

(3).虛函數(shù)不能使用返回類型推導。

C++11中auto的使用:一文詳解C++11中auto的使用

C+11中decltype的使用:一文詳解C++11中decltype的使用

GitHub:https://github.com/fengbingchun/Messy_Test

到此這篇關于詳解C++14中返回類型推導的使用的文章就介紹到這了,更多相關C++14返回類型推導內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • C語言中的結構體內嵌函數(shù)用法

    C語言中的結構體內嵌函數(shù)用法

    這篇文章主要介紹了C語言中的結構體內嵌函數(shù)用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • C語言一維數(shù)組初步學習筆記

    C語言一維數(shù)組初步學習筆記

    這篇文章主要介紹了C語言一維數(shù)組初步學習筆記,包括指針訪問數(shù)組等重要知識點,需要的朋友可以參考下
    2016-05-05
  • C語言中的指針新手初階指南

    C語言中的指針新手初階指南

    指針是C語言的靈魂,精華之所在,指針強大而危險,用得好是一大利器,用得不好是一大潛在危害,下面這篇文章主要給大家介紹了C語言中指針的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2021-10-10
  • C++實現(xiàn)紅黑樹應用實例代碼

    C++實現(xiàn)紅黑樹應用實例代碼

    紅黑樹它一種特殊的二叉查找樹,這意味著它滿足二叉查找樹的特征,但是也有許多自己的特性,這篇文章主要給大家介紹了關于C++實現(xiàn)紅黑樹的相關資料,需要的朋友可以參考下
    2021-11-11
  • C語言十進制轉二進制代碼實例

    C語言十進制轉二進制代碼實例

    這篇文章主要介紹了C語言十進制轉二進制代碼實例,并且轉換后會統(tǒng)計二進制1的個數(shù),實例簡單明了,需要的朋友可以參考下
    2014-06-06
  • C++實現(xiàn)LeetCode(148.鏈表排序)

    C++實現(xiàn)LeetCode(148.鏈表排序)

    這篇文章主要介紹了C++實現(xiàn)LeetCode(148.鏈表排序),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-07-07
  • va_list(),va_start(),va_arg(),va_end() 詳細解析

    va_list(),va_start(),va_arg(),va_end() 詳細解析

    這些宏定義在stdarg.h中,所以用到可變參數(shù)的程序應該包含這個頭文件.下面我們寫一個簡單的可變參數(shù)的函數(shù),該函數(shù)至少有一個整數(shù)參數(shù),第二個參數(shù)也是整數(shù),是可選的.函數(shù)只是打印這兩個參數(shù)的值
    2013-09-09
  • C++中的數(shù)組引用和指針引用

    C++中的數(shù)組引用和指針引用

    這篇文章主要介紹了C++中的數(shù)組引用和指針引用詳細的相關資料,需要的朋友可以參考下面文章內容
    2021-09-09
  • C++中strlen(),sizeof()與size()的區(qū)別

    C++中strlen(),sizeof()與size()的區(qū)別

    本文主要介紹了C++中strlen(),sizeof()與size()的區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-05-05
  • C語言實現(xiàn)猜數(shù)字游戲的兩種方法

    C語言實現(xiàn)猜數(shù)字游戲的兩種方法

    猜數(shù)字小游戲是我們大多數(shù)人學習C語言時都會了解到的一個有趣的C語言小游戲,本文就詳細的介紹一下,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01

最新評論

穆棱市| 晋城| 安远县| 霸州市| 孝感市| 金平| 东至县| 怀集县| 丹巴县| 安图县| 泽州县| 巩留县| 平乡县| 桦川县| 西城区| 正安县| 罗源县| 黄山市| 贵南县| 刚察县| 修文县| 舒城县| 朔州市| 东乡县| 伊金霍洛旗| 象州县| 岳西县| 新源县| 横峰县| 马关县| 泰来县| 随州市| 赞皇县| 锡林郭勒盟| 米脂县| 汤阴县| 台中县| 宿松县| 海盐县| 云浮市| 剑川县|