c++模板后置返回类型

#include "iostream"

using namespace std;


template <typename T> auto compare(const T &t1,const T &t2) -> typename remove_reference<T>::type {
    return t1 > t2 ? t1 : t2;
};


template <typename T> auto compare1(const T &t1,const T &t2) -> typename remove_reference<decltype(t1)>::type {
    return t1 > t2 ? t1 : t2;
};

int main(){


    auto a = compare<int>(73,928);
    auto b = compare1<int>(73,928);
    std::cout << " a = " << a <<std::endl;
    std::cout << " b = " << b <<std::endl;
 
    int (*pf)(const int&,const int&) = compare;
    auto c = pf(73,92);
    std::cout << " c = " << c <<std::endl;
    
    return 0;
    
}

编译

$ g++ func.cpp -std=c++11

结果:

$ ./a.exe
 a = 928
 b = 928
 c = 92

猜你喜欢

转载自blog.csdn.net/wulong710/article/details/81736148
今日推荐