在C++14中允许使用type deduction用于函数参数和函数返回值
Return Type Deduction in C++11
#include <iostream>
using namespace std;
auto AutoFunctionFromReturn(int parameter) -> int
{
return parameter;
} int main()
{
auto value = AutoFunctionFromReturn();
cout << value << endl;
return ;
}
Deducing return types for C++11 template functions
#include <iostream>
using namespace std; template <typename T>
auto AutoFunctionFromParameter(T parameter) -> decltype(parameter)
{
return parameter;
} int main()
{
auto value = AutoFunctionFromParameter();
cout << value << endl;
return ;
}
In order to reduce the verbose code
Using auto to Deduce Return Type on a Template Function C++14
#include <iostream>
using namespace std; template <typename T>
auto AutoFunctionFromParameter(T parameter)
{
return parameter;
} int main()
{
auto value = AutoFunctionFromParameter();
cout << value << endl;
return ;
}