关键词:特化,全特化,偏特化;模板参数推导;函数重载
中文链接:
https://www.ibm.com/developerworks/mydeveloperworks/blogs/12bb75c9-dfec-42f5-8b55-b669cc56ad76/entry/c__e6_a8_a1_e6_9d_bf__e7_a9_b6_e7_ab_9f_e4_bb_80_e4_b9_88_e6_98_af_e7_89_b9_e5_8c_96?lang=zh&cmp=dw&cpb=dw&ct=dwcom&cr=dwcom&ccy=cn
英文链接:
https://www.ibm.com/developerworks/mydeveloperworks/blogs/5894415f-be62-4bc0-81c5-3956e82276f3/entry/c_templates_what_s_all_this_specialization_about_anyway8?lang=en
我已经制定了一个计划,希望可以定期地在博客上更新文章。我将探讨编译器如何处理模板等这类有意思的事情,并佐以实例。
作为一个编译器开发人员,我倾向于用一些小的例子来显示或者测试编译器是如何工作的,而不是来指导你如何在一个应用程序中使用编译器的某个功能。或许会有些人觉得这是有趣的事情。关于这个话题,我有很多想法,也希望大家能多提供建议。
struct t1{}; struct t2{}; struct t3{}; void func(t1 arg){ printf("called t1\n"); } void func(t2 arg){ printf("called t2\n"); } void func(t3 arg){ printf("called t3\n"); } int main(void) { t1 x1; t2 x2; t3 x3; func(x1); func(x2); func(x3); return 0; }
#include <iostream> #include <typeinfo> struct t1{}; struct t2{}; struct t3{}; using namespace std; template <class A, class B, class C> void func(A a1, B a2, C a3) { cout << "A: " << typeid(a1).name() << endl; cout << "B: " << typeid(a2).name() << endl; cout << "C: " << typeid(a3).name() << endl; } int main(void) { t1 x1; t2 x2; t3 x3; func(x1,x2,x3); return 0; }
#include <iostream> #include <typeinfo> using namespace std; struct t1{}; struct t2{}; struct t3{}; template <class A, int I> struct container{ void callMe(){ cout << "primary A: " << typeid(A).name() << " I: " << I << endl; } }; int main(void) { container<t1,10> test; test.callMe(); return 0; }
#include <iostream>
#include <typeinfo>
using namespace std;
struct t1{}; struct t2{}; struct t3{};
template <class A, int I> struct container{
void callMe(){
cout << "primary A: " << typeid(A).name() << " I: " << I << endl;
}
};
template <> struct container<t3,99>{
void callMe(){
cout << "complete specialization t3, 99" << endl;
}
};
int main(void)
{
container<t1,10> test1;
test1.callMe();
container<t3,99> test2;
test2.callMe();
return 0;
}
运行结果#include <iostream> #include <typeinfo> using namespace std; struct t1{}; struct t2{}; struct t3{}; template <class A, int I> struct container{ void callMe(){ cout << "primary A: " << typeid(A).name() << " I: " << I << endl; } }; template <class A1> struct container<A1,25>{ void callMe(){ cout << "partial specialization" << typeid(A1).name() << " and 25 " << endl; } }; template <> struct container<t3,99>{ void callMe(){ cout << "complete specialization t3, 99" << endl; } }; int main(void) { container<t1,10> test1; test1.callMe(); container<t3,99> test2; test2.callMe(); container<t2,25> test3; test3.callMe(); container<t3,25> test4; test4.callMe(); return 0; }
#include <iostream> #include <typeinfo> using namespace std; struct t1{}; struct t2{}; struct t3{}; template <class A, int I> struct container{ void callMe(){ cout << "primary A: " << typeid(A).name() << " I: " << I << endl; } }; template <int I1> struct container<t3,I1>{ void callMe(){ cout << "partial specialization t3 and " << I1 << endl; } }; template <> struct container<t3,99>{ void callMe(){ cout << "complete specialization t3, 99 " << endl; } }; int main(void) { container<t1,10> test1; test1.callMe(); container<t3,99> test2; test2.callMe(); container<t3,75> test3; test3.callMe(); container<t3,25> test4; test4.callMe(); return 0; }