c++中template函数

#include "iostream"

using namespace std;


template <typename T> T calc(const T a,const T b){
    T c = a + b;
    return c;
}

int main(){

    auto e = calc(83,7);
    std::cout << "e = " << e << std::endl;

    auto s = calc(std::string("what "), std::string("can i do "));
    std::cout << "s = " << s << std::endl;

    return 0;
}

结果:

andrew@andrew-PC /cygdrive/e/study/cpp/template
$ ./a.exe
e = 90
s = what can i do

猜你喜欢

转载自blog.csdn.net/wulong710/article/details/81433237