Lauen_1

Stay foolish

2-4. Using auto with Functions

在C++14中允许使用type deduction用于函数参数和函数返回值

Return Type Deduction in C++11

 1 #include <iostream>
 2 using namespace std;
 3 auto AutoFunctionFromReturn(int parameter) -> int
 4 {
 5     return parameter;
 6 }
 7 
 8 int main()
 9 {
10     auto value = AutoFunctionFromReturn(1);
11     cout << value << endl;
12     return 0;
13 }

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(2);
    cout << value << endl;
    return 0;
}

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(2);
    cout << value << endl;
    return 0;
}

 

posted on 2016-11-06 15:49  Lauen_1  阅读(188)  评论(0编辑  收藏  举报

导航