C++ 模板特化
代码:
#include <iostream>
#include <cstring>
#include <memory>
using namespace std;
class AAA{
public:
AAA(){
}
~AAA(){
}
};
class BBB{
public:
BBB(){
}
~BBB(){
}
};
/* 模板类 */
template<typename ImplType>
class ExpImplPtr{
public:
ExpImplPtr(std::unique_ptr <ImplType>&& ptr)
: ptr_(ptr.release())
{
std::cout << "模板 通用版本 --- " << std::endl; }
private:
std::unique_ptr<ImplType> ptr_;
};
/* 模板特化 */
template<>
class ExpImplPtr<BBB> {
public:
ExpImplPtr(std::unique_ptr <BBB>&& ptr)
: ptr_(ptr.release())
{
std::cout << "模板 特例化 --- " << std::endl; }
private:
std::unique_ptr <BBB> ptr_;
};
int main(int argc, char const *argv[])
{
ExpImplPtr<AAA> aa(std::make_unique<AAA>()); //模板 泛化
std::cout << "----" << std::endl;
ExpImplPtr<BBB> bb(std::make_unique<BBB>()); //模板 特例化
return 0;
}