C++函数模板

it2022-05-05  181

#include <iostream> #include <string> #include <stdlib.h> using namespace std; //模板具体化 //优先级 //普通函数>模板具体化>函数模板 struct Node { int a; }; //函数模板 template<typename T> void fun(T t) { cout << "这是普通变量" << endl; cout << t << endl; } //模板具体化 template<> void fun<Node>(Node t) { cout << "这是结构体" << endl; cout << t.a << endl; } //模板具体化 template<> void fun<int>(int i_) { cout << "这是整型" << endl; cout << i_ << endl; } //普通函数 void fun(int t) { cout << "这是普通函数" << endl; cout << t << endl; } void main() { /*Node a = { 1202 }; fun(a);*/ fun(7892); system("pause"); }

最新回复(0)