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.

时间: 2024-11-10 21:29:50

c++11: trailing return type in functions(函数返回类型后置)的相关文章

c++标准14取消decltype推算函数返回类型

Table of Contents 1. c++11之前不支持auto关键字 2. c++11支持auto关键字 2.1. 但是不能自动推断函数返回类型 2.2. 使用-> decltype来声明返回类型 3. c++14让事情又回到简单 4. 我们该使用哪个c++版本 1 c++11之前不支持auto关键字 下面的代码在c++11中是不支持的 auto add(int a, int b) { int i = a + b; return i; } int main(int argc,char *

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

c++ 函数的返回类型,包括const 什么时候起作用呢? 函数返回值不想其立即修改的. 例子如下,这是一个简单的避免产生隐形返回变量的方法,abc 的函数返回是引用,main函数中第10行,++ 操作是基于 const int & 类型,所以会出错,但以后对改引用的操作不会受到const 约束. 这样的好处是避免了函数返回值与操作符的逻辑错误结合,例如下面的例子中函数返回的++,对于main 函数是不直观的,进一步的应用是在操作符重载方面,见下一情况说明. 1 const int & a

函数指针(函数指针作为函数形参/函数类型作为函数返回类型)

函数指针是指向函数的指针变量. 因此"函数指针"本身首先应是指针变量,只不过该指针变量指向函数.这正如用指针变量可指向整型变量.字符型.数组一样,这里是指向函数.如前所述,C在编译时,每一个函数都有一个入口地址,该入口地址就是函数指针所指向的地址.有了指向函数的指针变量后,可用该指针变量调用函数,就如同用指针变量可引用其他类型变量一样,在这些概念上是大体一致的.函数指针有两个用途:调用函数和做函数的参数. 1 #include<stdio.h> 2 int max(int

C++11:类型推导和追踪函数返回类型decltype

参考文章:https://blogs.oracle.com/pcarlini/entry/c_11_tidbits_decltype_part decltype 是 GCC 实现的第一个 C++ 11 新特性.它实际上起源于一个相当古老的 GNU 扩展关键字-- __typeof__.这个非标准关键字也能够在 C 语言中使用,GNU Compiler Collection 的专业用户可能对它更熟悉一些.2008 年,GCC 4.3.x 就实现了这个特性,同时去除了 __typeof__ 的一些缺

vector作为函数返回类型

#include <vector> #include <iostream> using namespace std; vector<int> fun1(int num) { vector<int> values; for (int j = 0; j < num; j++) { values.push_back(j); } return values; } int main() { vector<int> myvector; int i; c

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

1 后台获取请求数据 1.1 提出问题 前台发送请求的方式有哪些 后台如何获取这些请求的参数 1.2 前台发送请求的方式 GET.POST.AJAX 点睛:如果不指定请求方式,浏览器默认使用GET请求 点睛:进入登录页面的请求和提交登录信息的请求使用的路径都是一样的,只不过前往登录页面的请求是GET请求,服务器返回的是一个静态的页面:当录入登录信息点击确定后就会向后台发送一个POST请求,后台经过逻辑处理后,如果登录信息正确就会返回一个静态主页面(注意:虽然这两个请求都是使用的一样的路径,但是我

auto function -&gt; return type 当不能从{}内推断类型时

示例: 1 template<class F, class... Args> 2 auto ThreadPool::enqueue(F&& f, Args&&... args) 3 -> std::future<typename std::result_of<F(Args...)>::type> result_of可以推断函数F(Args...)的返回值类型, auto ->表明函数返回类型为std::future<ty

return在返回类型为void方法中的妙用

序:我们都知道return是用来返回一个值给主调函数的.但是其实return也有另外一个作用,就是提前结束当前函数的后续执行. -------------------------------------------------------------------------------------Copyright ©2016 ZRY 好了,下面进入正题.... 一.很多情况下,我们在代码中都会遇到需要判断多个条件的情况(需要嵌套多个if else). 例:每满足一个条件都有一个if else(

初级函数返回值

返回值 什么是函数返回值:函数执行后的结果外部需要使用的时候,我们不能直接给予,是需要通过return返回. 1.执行函数完毕之后,返回的数据 2.有return的函数就有返回值,反之则没有返回undefined 3.把函数内部的值赋值给外边 var j=fn(2,3); console.log(j); function fn(a,b) { var c=a+b; // 如果我们想把函数内部的值赋值给外部,必须使用return; //如果没有return或则return没有值,函数返回undefi