写C++模板函数的两种形式

#include <iostream>

template <typename T>
auto f1(const T& x) {
    std::cout << x << std::endl;
};

auto f2 = [](const auto& x){
    std::cout << x << std::endl;
};

int main(int argc, char** argv)
{ 
    int x1 = 1;
    double x2 = 1.0;
    char x3 = '1';
    std::string x4 = "1";
    f1(x1);
    f1(x2);
    f1(x3);
    f1(x4);
    f2(x1);
    f2(x2);
    f2(x3);
    f2(x4);
}

  

posted @ 2023-05-10 14:06  南乡水  阅读(11)  评论(0编辑  收藏  举报