template<typename T> T add(const T&a, const T &b) { return a+b; } void func(int(*ptrfun)(const int& a, const int& b)) //后面两个参数的声明必须写 { cout<<"int"<<endl; } void func(string(*ptrfun)(const string& a,const string& b)) { cout<<"string"<<endl; } void main() { func(add<int>); }
#include <iostream> template<typename T0, typename T1, typename T2, typename T3, typename T4> T2 func(T1 v1, T3 v3, T4 v4); int main() { double sv2; using namespace std; sv2 = func<double, int, int>(1, 2, 3); cout << " sv2: " << sv2 << endl; sv2 = func<double, int, int>(1, 2, 3); cout << " sv2: " << sv2 << endl; sv2 = func<double, int, int>(1, 0.1, 0.1); cout << " sv2: " << sv2 << endl; sv2 = func<int, double, double>(0.1, 0.1, 0.1); cout << " sv2: " << sv2 << endl; } template<typename T0, typename T1, typename T2, typename T3, typename T4> T2 func(T1 v1, T3 v3, T4 v4) { T0 static sv0 = T0(0); T2 static sv2 = T2(0); std::cout << " v1: " << v1 << " v3: " << v3 << " v4: " << v4 << " || sv0: " << sv0;
T2 v2 = sv2; sv0 -= 1; sv2 -= 1; return v2; }
// 文件名caller1.cpp #include <iostream> template<typename T> void func(T const &v) { std::cout << "func1: " << v << std::endl; } void caller1() { func(1); func(0.1); } // ====================================== // 文件名caller2.cpp #include <iostream> template<typename T> void func(T const &v) { std::cout << "func2: " << v << std::endl; } void caller2() { func(2); func(0.2f); } // ====================================== // 文件名main.cpp void caller1(); void caller2(); int main() { caller1(); caller2(); return 0; }
template<typename T> T add(const T&a, const T &b) { return a+b; } void func(int(*ptrfun)(const int& a, const int& b),const int& a, const int&b) //后面两个参数的声明必须写 { ptrfun=add<int>; cout<<ptrfun(a,b)<<endl; } void func(string(*ptrfun)(const string& a,const string& b),const string& a,const string& b) { ptrfun=add<string>; cout<<ptrfun(a,b)<<endl; } void main() { func(add,1,2); func(add,"hello","world"); }
template<typename T> T add(const T&a, const T &b) { return a+b; } void func(int(*ptrfun)(const int& a, const int& b)) //后面两个参数的声明必须写 { cout<<"int"<<endl; } void func(string(*ptrfun)(const string& a,const string& b)) { cout<<"string"<<endl; } void main() { func(add<int>); }
template<typename T> class my_stack; // 前置栈类模板声明 template<typename T> class list_node { T value; list_node *next; // 私有构造函数,只能由其友类构造 list_node(T const &v, list_node *n) :value(v), next(n) {} // 友类必须是类模板my_stack的实例 friend class my_stack<T>; }; template<typename T=int> class my_stack { private: typedef list_node<T> node_type;//<T>不可省略 node_type *head; // my_stack不可复制构造,也不可赋值 my_stack operator=(my_stack const &) {} my_stack(my_stack const &s) {} public: // 构造与析构 my_stack() : head(0) {} ~my_stack() {while (!empty()) pop();} // 在类模板内实现的成员函数模板 bool empty() const {return head == 0;} T const& top() const { if (empty()) throw std::runtime_error("stack is empty."); return head->value; } void push(T const &v) {head = new node_type(v, head);} // 成员函数声明,将在类模板外实现 void pop(); }; // 在类模板外实现的成员函数模板 template<typename T> void my_stack<T>::pop() { if (head) { node_type *tmp = head; head = head->next; delete tmp; } } //子类模板:计数栈 template<typename T=int> class count_stack : public my_stack<T> { typedef my_stack<T> base_type; // 非常有用的typedef unsigned size; public: count_stack() : base_type(), size(0) {} void push(T const &v) { base_type::push(v); size++; } void pop() { if (size > 0) { base_type::pop(); size--; } } unsigned getSize() const {return size;} };
#include "stack.h" int main() { my_stack<> mystack; mystack.push(2); cout<<mystack.top()<<endl; count_stack<> costack; costack.push(9); costack.push(8); costack.push(7); cout<<costack.getSize()<<endl; return 0; }
struct normal_class { int value; template<typename T> void set(T const &v) {value = int(v);} template<typename T> T get(); }; template<typename T> T normal_class::get() { return T(value); }
template<typename T0> struct a_class_template { T0 value; template<typename T1> void set(T1 const &v){value = T0(v);} template<typename T1> T1 get(); }; // 类模板的成员函数模板在类模板外的实现方法 template<typename T0> template<typename T1> T1 a_class_template<T0>::get() { return T1(value); }
template<typename T> class the_class { public: static int id; the_class() {++id;} static T getid(){return T(id)/2;} }; template<typename T> int the_class<T>::id = 1;
#include "the_class.h" void call() { the_class<int> c; std::cout << c.id << std::endl; }
#include "the_class.h" void call1() { the_class<int> c; std::cout << c.id << std::endl; }
void call(); void call1(); int main() { call(); call1(); }