c++11: trailing return type in functions(函数返回类型后置)
2015-05-09 14:30
363 查看
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:类型推导和追踪函数返回类型decltype
- C++11 理解 (二十八) 之 用于计算函数对象返回类型的统一方法
- 【C++11学习笔记】返回类型后置语法
- C++11 FAQ中文版:返回值类型后置语法
- C++11 FAQ中文版:返回值类型后置语法
- 【C++11学习笔记】返回类型后置语法
- 【C++11学习笔记】返回类型后置语法
- C++11特性--auto,decltype,返回类型后置,using=,nullptr
- C++11用于计算函数对象返回类型的统一方法
- 【C++】重载运算符号时的函数返回值类型问题
- 函数返回类型为引用的好处
- 返回类型是函数指针的函数指针 类比复杂的数组声明
- 关于C#间接继承的一些思考:如何修改继承函数的返回值类型
- 将“引用”作为函数返回值类型的格式、好处和需要遵守的规则
- 杂货边角(13):利用可变参数模板根据函数指针得到返回值类型和形参类型
- 以结构类型作为函数的返回值
- 函数中有多个类型的返回值 解决
- 返回值类型为unsigned long long的函数永远返回为0的问题解决
- 在Oracle的函数中,返回表类型
- 函数的返回类型为指针类型时的若干思考(字符串常量问题)