详解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语言的数组与指针,本文给大家介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下,希望能给你带来帮助
    2021-08-08
  • C++ 中回文数判断简单实例

    C++ 中回文数判断简单实例

    这篇文章主要介绍了C++ 中回文数判断简单实例的相关资料,需要的朋友可以参考下
    2017-05-05
  • 探讨C++中不能声明为虚函数的有哪些函数

    探讨C++中不能声明为虚函数的有哪些函数

    下面小编就为大家带来一篇探讨C++中不能声明为虚函数的有哪些函数。希望对大家有所帮助。一起跟随小编过来看看吧,祝大家游戏愉快哦
    2017-01-01
  • linux下基于C语言的信号编程实例

    linux下基于C语言的信号编程实例

    这篇文章主要介绍了linux下基于C语言的信号编程,实例分析了信号量的基本使用技巧与相关概念,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • C++中new和delete匹配使用过程详解

    C++中new和delete匹配使用过程详解

    关于 new 和 delete 的使用相信大家并不陌生,可是为什么使用 new 的时候要用 delete,使用 new[] 的时候又要用 delete[]呢?本文就来和大家详细说说
    2023-02-02
  • 在C/C++项目中合理使用宏详解

    在C/C++项目中合理使用宏详解

    这篇文章主要介绍了在C/C++项目中合理使用宏详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • C++实现双向链表代码分析

    C++实现双向链表代码分析

    这篇文章主要介绍了C++实现双向链表代码分析,前面文章分析了单向链表,这篇文章就来给大家分享双链表的实现吧,需要的朋友可以参考一下
    2022-03-03
  • 详谈全排列next_permutation() 函数的用法(推荐)

    详谈全排列next_permutation() 函数的用法(推荐)

    下面小编就为大家带来一篇详谈全排列next_permutation() 函数的用法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-03-03
  • C语言绘制三角函数曲线

    C语言绘制三角函数曲线

    这篇文章主要为大家详细介绍了C语言绘制三角函数曲线,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-06-06
  • 基于OpenCV读取摄像头实现单个人脸验证MFC程序

    基于OpenCV读取摄像头实现单个人脸验证MFC程序

    这篇文章主要为大家详细介绍了基于OpenCV读取摄像头实现单个人脸验证MFC程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-08-08

最新评论