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 ...
随机推荐
- 自定义控件 带描边的TextView
使用 public class MainActivity extends Activity { @Override protected void onCreate(Bundle sav ...
- SQL Profiler工具简介
一.SQL Profiler工具简介 SQL Profiler是一个图形界面和一组系统存储过程,其作用如下: 图形化监视SQL Server查询: 在后台收集查询信息: 分析性能: 诊断像死锁之类的问 ...
- CSS选择器列表
h1 类型选择器 选择元素的一个类型 .className 类选择器 以class属性的值来选择元素,可以在一个页面中出现多个 #idName ID选择器 以id属性的值来选择元素,在页面中是唯一的, ...
- 使用 Intel HAXM 为 Android 模拟器加速,媲美真机(转)
源:http://www.cnblogs.com/beginor/archive/2013/01/13/2858228.html 慢的问题. Intel HAXM (Hardware Accelera ...
- ADB命令与monkey
adb devices查看已连接的设备 adb install package.apk adb shell monkey 1000 随机操作1000次当次操作可能会 adb无法使用,提示error: ...
- div+css不间断滚动字幕
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 莫队算法学习笔记【BZOJ2038:小Z的袜子】【SPOJ3267:D-query】
很久以前傻乎乎地看来源奇怪的资料的时候被各种曼哈顿弄晕了. 然后现在学会的是分块方法.另新创一个分块方法. 让我们考虑这样一个区间询问问题…… 它有如下的性质: 0,n个数,Q个询问. 1,它没有修改 ...
- 近期Responsive web design项目经验分享
关于meta <meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, user ...
- 乱七八糟 Nodejs 系列一:试水
看Nodejs比较舒服的人,个人认为主要是: 以前是后端,转成前端的人: 前端巨牛的人: 后端巨牛的人: 巨牛的人... 当然还有我这种,脑抽不止的人~~ 不过学习的过程中发现,如果不是上来就用exp ...
- C程序设计语言练习题1-3
练习1-3 修改温度转换程序,使之能在转换表的顶部打印一个标题. 代码如下: #include <stdio.h> // 包含标准库的信息. int main() // 定义名为main的 ...