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. Java基础知识强化32:String类之String类的判断功能

    1. String类的判断功能: boolean equals (Object obj ) boolean equalsIgnoreCase (String str ) boolean contain ...

  2. USB 管道 && 端点

    管道是对主机和usb设备间通信流的抽象.      管道和usb设备中的端点一一对应,一个usb设备含有多少个端点,其和主机进行通信时就可以使用多少条管道,且端点的类型决定了管道中数据的传输类型.  ...

  3. vi & vim 基本指令(持续更新ing)

    Abstract:1) 文本编辑模式:                      --INSERT--2)一般模式:                      --i.o.a.R--3)命令行命令模式 ...

  4. C# XML流操作简单实例

    这里我们先介绍操作XML文件的两个对象:XmlTextReader和XmlTextWriter打开和读取Xml文件使用到的对象就是XmlTextReader对象.下面的例子打开了与程序在同一路径下的一 ...

  5. C#中静态构造函数含义及使用

    static以前都接触过,可是最近才发现了还有静态类的写法,也可能是以前没太注意了,所以自己去研究了一下! 1.什么是构造函数: 1.1 例如:static  Class{} 1.2 使用静态函数的注 ...

  6. Ubuntu下安装搜狗拼音输入法

    Ubuntu默认输入法是ibus输入法,其实用着也可以了,但是说句实话在某些情况下真的不怎么智能.习惯了搜狗所以,查阅资料测试成功后整理如下, 1.安装搜狗拼音 sudo apt-add-reposi ...

  7. 用jQuery的ajax请求一般处理程序返回json数据

    1.web页面代码: 注意事项: dataType类型一定要写成json. 2.一般处理程序代码: 注意事项: ContentType类型写成"application/json"或 ...

  8. .net framework3.0 以上版本开发的软件乱码问题

    首先介绍一下:WPF是微软新一代图形系统,运行在.NET Framework 3.0及以上版本下,为用户界面.2D/3D 图形.文档和媒体提供了统一的描述和操作方法.基于DirectX 9/10技术的 ...

  9. javaScript中将时间戳转换成日期格式

    function DateFormt(time, format) { ); var o = { , "d+": testDate.getDate(), "h+" ...

  10. MySQL主从问题

    Mysql数据库主从心得整理      管理mysql主从有2年多了,管理过200多组mysql主从,几乎涉及到各个版本的主从,本博文属于总结性的,有一部分是摘自网络,大部分是根据自己管理的心得和经验 ...