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. QT程序启动界面的使用

    当程序的初始化工作比较多,程序可能启动较长时间后,窗口才会显示出来,用户没准会抱怨程序响应的慢. 为了改善用户体验,最好在程序初始化这段时间显示logo,或者其他信息提示用户程序已启动.QT提供了QS ...

  2. 详解C++ friend关键字

    1. 为什么要使用友元? 通常对于普通函数来说,要访问类的保护成员是不可能的,如果想这么做那么必须把类的成员都生命成为 public( 共用的) ,然而这做带来的问题遍是任何外部函数都可以毫无约束的访 ...

  3. 服务 进程间通讯 IPC AIDL Parcelable 简介

    1.IBinder和Binder是什么鬼? 我们来看看官方文档怎么说: 中文翻译:  IBinder是远程对象的基本接口,是为了高性能而设计的轻量级远程调用机制的核心部分. 但他不仅用于远程调用,也用 ...

  4. 手贱随手在Linux敲了 as 命令,出不来了

    手贱随手在Linux敲了  as  命令,出不了命令,问问度娘吧,得到下列资料 as命令 GNU组织推出的一款汇编语言编译器,它支持多种不同类型的处理器.语法as(选项)(参数)选项-ac:忽略失败条 ...

  5. Sass简介,安装环境,Sass的语法格式及编译调试

    什么是 CSS 预处理器? 定义:CSS 预处理器定义了一种新的语言,其基本思想是,用一种专门的编程语言,为 CSS 增加了一些编程的特性,将 CSS 作为目标生成文件,然后开发者就只要使用这种语言进 ...

  6. 解决Android SDK Manager下载太慢问题(转)

    1.打开android sdk manager 2.打开tool->options,如图所示 3.将Proxy Settings 里的HTTP Proxy Server和HTTP Proxy P ...

  7. 【nodejs学习】3.进程管理及异步编程

    进程管理 1.调用终端命令实现目录目录拷贝 var child_procress = require('child_procress'); var util = require('util'); fu ...

  8. iOS ui界面vtf 开发

    addConstraints 添加约束的步奏 添加控件到view中 设置translateResizeLayoutintoautolayout = false 添加约束 注意 约束 : 出现 有父子关 ...

  9. 网站压缩数据 GZIP

    //1.被压缩数据 String str="Hello 你好Hello 你好Hello 你好Hello 你好Hello 你好Hello 你好Hello 你好Hello 你好Hello 你好H ...

  10. mysql 5.6 参数详解

    系统变量提供的是各种与服务器配置和功能有关的信息.大部分的系统变量都可以在服务器启动时进行设置.在运行时,每一个系统变量都拥有一个全局值或会话值,或者同时拥有这两个值.许多系统变量都是动态的,也就是说 ...