详解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: 虚成员函数不应具有含“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).函数返回类型为auto或decltype(auto):如果有多个return语句,它们必须全部推导出相同的类型,否则会编译error。

(3).虚函数不能使用返回类型推导。

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

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

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

到此这篇关于详解C++14中返回类型推导的使用的文章就介绍到这了,更多相关C++14返回类型推导内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C语言中的结构体内嵌函数用法

    C语言中的结构体内嵌函数用法

    这篇文章主要介绍了C语言中的结构体内嵌函数用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • C语言一维数组初步学习笔记

    C语言一维数组初步学习笔记

    这篇文章主要介绍了C语言一维数组初步学习笔记,包括指针访问数组等重要知识点,需要的朋友可以参考下
    2016-05-05
  • C语言中的指针新手初阶指南

    C语言中的指针新手初阶指南

    指针是C语言的灵魂,精华之所在,指针强大而危险,用得好是一大利器,用得不好是一大潜在危害,下面这篇文章主要给大家介绍了C语言中指针的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2021-10-10
  • C++实现红黑树应用实例代码

    C++实现红黑树应用实例代码

    红黑树它一种特殊的二叉查找树,这意味着它满足二叉查找树的特征,但是也有许多自己的特性,这篇文章主要给大家介绍了关于C++实现红黑树的相关资料,需要的朋友可以参考下
    2021-11-11
  • C语言十进制转二进制代码实例

    C语言十进制转二进制代码实例

    这篇文章主要介绍了C语言十进制转二进制代码实例,并且转换后会统计二进制1的个数,实例简单明了,需要的朋友可以参考下
    2014-06-06
  • C++实现LeetCode(148.链表排序)

    C++实现LeetCode(148.链表排序)

    这篇文章主要介绍了C++实现LeetCode(148.链表排序),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • va_list(),va_start(),va_arg(),va_end() 详细解析

    va_list(),va_start(),va_arg(),va_end() 详细解析

    这些宏定义在stdarg.h中,所以用到可变参数的程序应该包含这个头文件.下面我们写一个简单的可变参数的函数,该函数至少有一个整数参数,第二个参数也是整数,是可选的.函数只是打印这两个参数的值
    2013-09-09
  • C++中的数组引用和指针引用

    C++中的数组引用和指针引用

    这篇文章主要介绍了C++中的数组引用和指针引用详细的相关资料,需要的朋友可以参考下面文章内容
    2021-09-09
  • C++中strlen(),sizeof()与size()的区别

    C++中strlen(),sizeof()与size()的区别

    本文主要介绍了C++中strlen(),sizeof()与size()的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • C语言实现猜数字游戏的两种方法

    C语言实现猜数字游戏的两种方法

    猜数字小游戏是我们大多数人学习C语言时都会了解到的一个有趣的C语言小游戏,本文就详细的介绍一下,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01

最新评论