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(函数返回类型后置)的更多相关文章

  1. C++11 类型后置语法

    #include <iostream> #include <typeinfo> #include <type_traits> using namespace std ...

  2. vector作为函数返回类型

    在实际的操作中,我们经常会碰到需要返回一序列字符串或者一列数字的时候,以前会用到数组来保存这列的字符串或者数字,现在我们可以用vector来保存这些数据.但是当数据量很大的时候使用vector效率就比 ...

  3. c/c++: c++函数返回类型什么情况带const

    c++ 函数的返回类型,包括const 什么时候起作用呢? 函数返回值不想其立即修改的. 例子如下,这是一个简单的避免产生隐形返回变量的方法,abc 的函数返回是引用,main函数中第10行,++ 操 ...

  4. MSSQLSERVER数据库- 字符串分割函数返回类型表

    遇到这样一个问题,存储在数据库的数据是一串字符串如:1,2,3,4,5,6.想把这串字符串进行转变成一个表格,如下: 1 2 3 4 5 6 就是这样一个问题,有人同事,写了一个这样的封装函数,这样就 ...

  5. Flask04 后台获取请求数据、视图函数返回类型、前台接受响应数据

    1 后台获取请求数据 1.1 提出问题 前台发送请求的方式有哪些 后台如何获取这些请求的参数 1.2 前台发送请求的方式 GET.POST.AJAX 点睛:如果不指定请求方式,浏览器默认使用GET请求 ...

  6. onkeypress 在js函数返回false后没有反应

    一.解决方案: 把 onkeypress = "function()" 改为 onkeypress = "event.returnValue=function()&quo ...

  7. Trailing return types

    Trailing return types是C++11关于函数声明的语言特性之一,旨在解决模版编程遇到的语法相关的问题,先看一个简单例子,感受一下什么是trailing return types: C ...

  8. C++ 函数模板的返回类型如何确定?

    函数模板 #include <iostream> // 多个参数的函数木板 template<typename T1, typename T2> T2 max(T1 a, T2 ...

  9. c++11 追踪返回类型

    c++11 追踪返回类型 返回类型后置:使用"->"符号,在函数名和参数列表后面指定返回类型. #define _CRT_SECURE_NO_WARNINGS #includ ...

随机推荐

  1. 新手们的GDI+绘制方格

    //绘制panel控件触发的事件 //不可在窗体加载时绘制方格        private void panel1_Paint(object sender, PaintEventArgs e)    ...

  2. js 判断微信浏览器(转)

    最近做很多HTML5的项目,很多页面会通过微信微博等SNS分享出去.在分享页面上提供公司APP的下载.但是在很多应用的浏览器中,点击下载链接无法下载应用.那么针对这些浏览器我们需要给用户提示从safa ...

  3. Linux 系统库函数coreleft 与sbrk简介

    coreleft   函数名: coreleft 功 能: 返回未使用内存的大小 用 法: unsigned coreleft(void); 程序例: #include <stdio.h> ...

  4. c - 冒泡/选择排序.

    #include <stdio.h> #define LEN 10 #define TRUE 1 #define FALSE 0 /* 对 10 个数进行排序 */ //选择排序法(值得注 ...

  5. javascrip中setTimeout和setInterval

    1: http://www.jb51.net/article/68258.htm 2: http://www.jb51.net/article/26679.htm

  6. JavaWeb学习笔记之Servlet(一)

    1. 引子: 当我们开始进入JavaWeb开发的学习时,我们就必须要和Servlet和HTTP这两个词进行打交道了,尤其是Servlet.即使到了后面使用JSP (我们知道JSP其本身就是一个Serv ...

  7. 数位dp整理

    数位dp的思想就在于递归,记录当前的某一个唯一状态,依次递归下去,要注意唯一. 数位dp常设的状态有当前位置,上一数字,是否具有前导零,是否有限制. 1.CodeForces 55DBeautiful ...

  8. poj1988 简单并查集

    B - 叠叠乐 Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:30000KB     64bit ...

  9. POJ 3273 Monthly Expense(二分查找+边界条件)

    POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...

  10. SQLServer分页

    1.为什么要分页? 当显示数据的时候,我们不会一下子把所有的数据都显示出来,比如说表中有一万条数据,难道我们要把一万条数据都一次性的显示出来吗?!即便显示给用户了,用户也看不过来.因此,不论是从效率的 ...