#include <iostream> using namespace std; template <class T> class test { public: T add(T a, T b) { return a + b; } }; template <class T> T Minus(T a, T b) { return a - b; } int main() { test<double> t1; cout << t1.add(3.5, 9.2) << endl; cout << Minus<int>(9, 7) << endl; return 0; }
#include <iostream> using namespace std; class test { public: typedef int* intpoint; struct str { int a; int b; }; class p { public: void fun() { cout << "hello world" << endl; } }; }; int main() { int a = 19; test::intpoint p = &a; //对于类中新类型的使用要通过::所属符 cout << *p << endl; test::str st1; st1.a = 15; cout << st1.a << endl; test::p p1; p1.fun(); return 0; }
#include <iostream> using namespace std; class test { public : typedef int* intpoint; }; template <class T> void fun() { T::intpoint a;//这里会发生二义性 test::intpoint b; //这就没有二义性 } int main() { return 0; }注意:上面代码中模板调用intpoint就发生了二义性,而类名调用intpoint就没有发生二义性,这个跟模板的特殊二次编译有关,当模板进行第一次语法编译的时候,不知道T是哪个具体的类,根本就不能确定intpoint是新类型还是静态成员。所以会发生二义性!
#include <iostream> using namespace std; class test { public : typedef int* intpoint; }; template <typename T> void fun() { typename T::intpoint a;//这里没有发生二义性 test::intpoint b; //这就没有二义性 } int main() { return 0; }
#include<iostream> using namespace std; template <typename T> void fun(T b) { cout << "b is " << b << endl; } void fun(int a) { cout << "a is " << a << endl; } void fun(...) { cout << "void fun" << endl; } int main() { int a = 12; fun(a); //重载时 普通函数最优先 double b = 0.01; fun(b); //函数模板其次 fun(a,b); //最后是可变参数函数 }
#include <iostream> using namespace std; template <typename T> bool checkpoint (T*) //这个函数模板 只接受指针类型的变量 { cout << "true" << endl; return true; } bool checkpoint (...) //这个可变参数函数 接受除了指针类型以外的变量 { cout << "false" << endl; return false; } int main() { int a; checkpoint(a); int* b; checkpoint(b); return 0; }c.虽然上面的解决方案已经解决这个问题,但是效率还不够高,因为它仍然在函数调用的建栈与退栈中浪费了时间。高效的解决方案如下:
#include <iostream> using namespace std; template <typename T> int checkpoint (T*) /*{ cout << "checkpoint (T*)" << endl; }*/; //这里完全可以是一个空函数 char checkpoint (...) /*{ cout << "checkpoint (...)" << endl; }*/; //这里完全可以是一个空函数 #define ispoint(v) (sizeof(checkpoint(v)) == sizeof(int)) int main() { int a; cout << ispoint(a) << endl; int *p; cout << ispoint(p) << endl; return 0; }注意:因为没有打算调用这个两个函数,所以这两个函数可以为空函数,代码中注释掉的函数内容是为了证明sizeof是真的没有调用函数用的。因为sizeof是在编译期间,进行处理的,sizeof(checkpoint(v))在编译期间就确定了调用那个函数,就可以通过函数返回值来判断调用那个函数,从而判断函数参数是否为指针。因为没有调用函数,只是在编译期间进行了判断,所以没有建栈退栈的时间!
原文:http://blog.csdn.net/mbh_1991/article/details/18940453