C++ 特定成员函数的部分特化 #包括 样板 B类{ 公众: void update(){std::cerr
标记将调用分派到C++ 特定成员函数的部分特化 #包括 样板 B类{ 公众: void update(){std::cerr,c++,templates,specialization,partial-specialization,C++,Templates,Specialization,Partial Specialization,标记将调用分派到update: #include <iostream> template <typename T1, typename T2> class B{ public: void update(){ std::cerr<<__PRETTY_FUNCTION__<<std::endl; } void func1(){ std::cerr<<__PRETTY_FUNCTION__<<std::endl;
update
:
#include <iostream>
template <typename T1, typename T2>
class B{
public:
void update(){ std::cerr<<__PRETTY_FUNCTION__<<std::endl; }
void func1(){ std::cerr<<__PRETTY_FUNCTION__<<std::endl; }
void func2(){ std::cerr<<__PRETTY_FUNCTION__<<std::endl; }
};
template <typename T1>
class B<T1, int>{
public:
void update(){ std::cerr<<__PRETTY_FUNCTION__<<"(specialization)"<<std::endl;}
};
int main(){
B<int, double> b1;
b1.update();
b1.func1();
B<int, int> b2;
b2.update();
//b2.func1();//there's no function 'func1' in B<int,int>
}
模板结构标记{};
样板
B类
{
公众:
无效更新()
{
返回更新(tag());
}
私人:
样板
无效更新(标记)
{
//专业化
}
样板
无效更新(标记)
{
//正常的
}
};
标记将呼叫发送到
更新
:
#include <iostream>
template <typename T1, typename T2>
class B{
public:
void update(){ std::cerr<<__PRETTY_FUNCTION__<<std::endl; }
void func1(){ std::cerr<<__PRETTY_FUNCTION__<<std::endl; }
void func2(){ std::cerr<<__PRETTY_FUNCTION__<<std::endl; }
};
template <typename T1>
class B<T1, int>{
public:
void update(){ std::cerr<<__PRETTY_FUNCTION__<<"(specialization)"<<std::endl;}
};
int main(){
B<int, double> b1;
b1.update();
b1.func1();
B<int, int> b2;
b2.update();
//b2.func1();//there's no function 'func1' in B<int,int>
}
模板结构标记{};
样板
B类
{
公众:
无效更新()
{
返回更新(tag());
}
私人:
样板
无效更新(标记)
{
//专业化
}
样板
无效更新(标记)
{
//正常的
}
};
可能重复的可能重复的