c++11: trailing return type in functions(函数返回类型后置)
In C++03, the return type of a function template cannot be generalized if the return type relies on those of the template arguments. Here is an example, mul(T a, T b) is a function template that calculates the product of a and b. Arguments a and b are of an arbitrary type T, which is not decided until the template instantiation. Thus, we cannot declare a return type for mul to generalize all the cases for a*b. If we must define such a function template, we have to introduce another template parameter as follows:
template<class T,class U>
U mul(T a, T b){
return a*b;
}
We have some trouble to instantiate this function template because we have to explicitly provide type U in the template instantiation. Can we use feature decltype to let the compiler deduce the result type automatically? See the following example:
template<class T>
decltype(a*b) mul(T a, T b){
return a*b;
}
This program lets the compiler deduce the return type of function template mul. Unfortunately, it doesn't work. The compiler is parsing codes from left to right, so it will issue an error message to indicate that variables a and b are used before their declarations.
To solve this problem, C++11 introduced a feature called trailing return types. Using this feature, the previous program can be rewritten as follows:
template<class T>
auto mul(T a, T b) -> decltype(a*b){
return a*b;
}
We specify the function return type after the declaration of parameter declarations. Composite symbol ->decltype(t1+t2) is called a trailing return type. The auto keyword is placed before the function identifier, which is the placeholder of the return type specifier. When a trailing return type is used, the placeholder return type must be auto. Meanwhile, the auto type specifier cannot be used in a function declaration without a trailing return type.
The biggest difference between ordinary functions and functions using trailing return types is whether to postpose the return types. See the following example:
auto max(int a, int b) -> int{}
Function max is using a trailing return type, which is equal to int max(int a, int b). This example shows a valid scenario of trailing return types, but it doesn't reflect the benefits of this feature. We can fully enjoy the convenience of generic programming by using trailing return types. See the following example:
#include <iostream>
using namespace std;
template<typename T1, typename T2>
auto sum(T1 & t1, T2 & t2) -> decltype(t1 + t2){
return t1 + t2;
}
int main(){
auto i = 1;
auto j = 1.3;
auto k = sum(a,b);
cout << c << endl;
}
This program doesn't contain any explicitly specified types. All the types are deduced by the compiler using auto type deductions and trailing return types, which saves a lot of programming efforts.
Another benefit of using trailing return types is the improvement of readability and maintainability of programs. See the following example:
template <class T> class tmp{
public:
int i;
};
tmp<int> (*(*foo())())() {
return 0;
}
Do you feel terrible after reading this program? Actually, foo is a function whose return type is a function pointer. The function pointer points to a function that returns a function pointer. Using trailing return types, the previous program can be rewritten as follows:
template <class T> class tmp{
public:
int i;
};
auto foo()->auto(*)()->tmp<int>(*)(){
return 0;
}
Do you see the magic of trailing return types in this example?
Besides the scenarios described above, trailing return types can also be used in function pointers, function references, member functions in classes/structures/class templates.
c++11: trailing return type in functions(函数返回类型后置)的更多相关文章
- C++11 类型后置语法
		
#include <iostream> #include <typeinfo> #include <type_traits> using namespace std ...
 - vector作为函数返回类型
		
在实际的操作中,我们经常会碰到需要返回一序列字符串或者一列数字的时候,以前会用到数组来保存这列的字符串或者数字,现在我们可以用vector来保存这些数据.但是当数据量很大的时候使用vector效率就比 ...
 - c/c++: c++函数返回类型什么情况带const
		
c++ 函数的返回类型,包括const 什么时候起作用呢? 函数返回值不想其立即修改的. 例子如下,这是一个简单的避免产生隐形返回变量的方法,abc 的函数返回是引用,main函数中第10行,++ 操 ...
 - MSSQLSERVER数据库- 字符串分割函数返回类型表
		
遇到这样一个问题,存储在数据库的数据是一串字符串如:1,2,3,4,5,6.想把这串字符串进行转变成一个表格,如下: 1 2 3 4 5 6 就是这样一个问题,有人同事,写了一个这样的封装函数,这样就 ...
 - Flask04 后台获取请求数据、视图函数返回类型、前台接受响应数据
		
1 后台获取请求数据 1.1 提出问题 前台发送请求的方式有哪些 后台如何获取这些请求的参数 1.2 前台发送请求的方式 GET.POST.AJAX 点睛:如果不指定请求方式,浏览器默认使用GET请求 ...
 - onkeypress 在js函数返回false后没有反应
		
一.解决方案: 把 onkeypress = "function()" 改为 onkeypress = "event.returnValue=function()&quo ...
 - Trailing return types
		
Trailing return types是C++11关于函数声明的语言特性之一,旨在解决模版编程遇到的语法相关的问题,先看一个简单例子,感受一下什么是trailing return types: C ...
 - C++ 函数模板的返回类型如何确定?
		
函数模板 #include <iostream> // 多个参数的函数木板 template<typename T1, typename T2> T2 max(T1 a, T2 ...
 - c++11 追踪返回类型
		
c++11 追踪返回类型 返回类型后置:使用"->"符号,在函数名和参数列表后面指定返回类型. #define _CRT_SECURE_NO_WARNINGS #includ ...
 
随机推荐
- USB HID介绍
			
HID是一种USB通信协议,无需安装驱动就能进行交互,在学习HID之前,先来复习一下USB协议的相关内容. USB设备描述符-概述 当插入USB设备后,主机会向设备请求各种描述符来识别设备.那什么是设 ...
 - jqueryMobile中select样式自定义
			
要去掉引入的jqueryMobile给下拉框组件的样式,有两种办法. 第一种:全局的去掉所有的下拉框样式: <link rel="stylesheet" href=" ...
 - NHIBERNATE之映射文件配置说明(转载4)
			
二十.自定义值类型 开发者创建属于他们自己的值类型也是很容易的.比如说,你可能希望持久化Int64类型的属性, 持久化成为VARCHAR 字段.NHibernate没有内置这样一种类型.自定义类型 ...
 - IE9的window.showmodaldialog显示问题
			
<html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat=&quo ...
 - ORACLE网络配置大全没有比这个更详细的【weber出品】
			
一.起篇 现在怎么说也是互联网时代,数据库也要联网,很多朋友学习Oracle的时候无外乎搭建的是以下两种学习环境: 1.直接在windows环境下安装Oracle后直接sqlplus连接. 2.在wi ...
 - SignalR小计
			
微软官方例子地址:http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr-a ...
 - java日期操作
			
//字符串转日期 public static void dt7() throws ParseException { String str_date="2015---08---08" ...
 - polya定理小结
			
polya的精髓就在与对循环节的寻找,其中常遇到的问题就是项链染色类问题. 当项链旋转时有n种置换,循环节的个数分别是gcd(n, i); 当项链翻转时有n种置换,其中当项链珠子数位奇数时,循环节的个 ...
 - MFC 点击改变文本 加法计时器 伸缩窗体
			
1.添加所需控件,设置ID:labNum1,txtNum1.txtNum2.txtNum3.btnAdd,并将labNum1的属性Notify->true(控件在被单击或双击时可以发送消息) 2 ...
 - C++ 知识点 2
			
基本类型常量 const int a; int const a; const int *a; int * const a; int const * a const; 之间的区别? const int ...