#include#include// 智能指针作为模板参数,并判断该智能指针管理的类型是否为指定类的子类structBase{};structDerived:Base{};structDerived1{};// 使用enable_if和is_base_of结合判断,T::element_type为智能指针所管理的对象类型,需要加typenametemplate<typenameT,typename= std::enable_if_t<std::is_base_of_v<Base,typenameT::element_type>>>voidConvert(T elem){
std::cout<<"T is subClass of Base"<<std::endl;}// 利用static_asserttemplate<typenameT>voidConvert_Assert(T elem){static_assert(std::is_base_of_v<Base,typenameT::element_type>,"T is not subClass of Base");
std::cout<<"T is subClass of Base"<<std::endl;}intmain(){
std::shared_ptr<Derived> son = std::make_shared<Derived>();Convert(son);Convert_Assert(son);
std::shared_ptr<Derived1> notSon = std::make_shared<Derived1>();// Convert(notSon); error// Convert_Assert(notSon); errorreturn0;}