山东个人网站备案,地板网站建设方案,建设网站宝安区,设计师招聘网站推荐1、std::is_base_of简介
在现代 C 中#xff0c;模板元编程#xff08;Template Metaprogramming#xff09;是一种非常强大的编程技巧#xff0c;它让我们能够在编译期进行类型推导和约束。而 std::is_base_of 是一个重要的工具#xff0c;可以用来检查一个类型是否是另…1、std::is_base_of简介
在现代 C 中模板元编程Template Metaprogramming是一种非常强大的编程技巧它让我们能够在编译期进行类型推导和约束。而 std::is_base_of 是一个重要的工具可以用来检查一个类型是否是另一个类型的基类。std::is_base_of 是 C11 引入的一个类型特性Type Trait位于头文件 type_traits 中。它用于检查一个类型 Base 是否是另一个类型 Derived 的基类。如果 Base 是 Derived 的基类或者两者是同一类型std::is_base_ofBase, Derived::value 将返回 true否则返回 false。它的定义原型如下
namespace std {template class Base, class Derivedstruct is_base_of;
}2、std::is_base_of 的特点
同一类型返回 truestd::is_base_ofBase, Base 会返回 true因为类型被视为自身的基类。支持多级继承即使继承链是多级的std::is_base_of 依然能够正确判断。例如
struct A {};
struct B : A {};
struct C : B {};std::is_base_ofA, C::value; // true对非派生关系返回 false如果两个类型之间没有继承关系std::is_base_of 会返回 false不需要对象std::is_base_of 只检查类型之间的关系而不依赖于实际的对象。检测 private 继承也返回 true无论继承是 public、protected 还是 private只要存在继承关系std::is_base_of 都会返回 true。
struct A {};
struct B : private A {};std::is_base_ofA, B::value; // true3、应用场景
简单例子
#include iostream
#include type_traitsstruct Base {};
struct Derived : Base {};
struct Unrelated {};int main() {std::cout std::boolalpha;// Base 是 Derived 的基类std::cout Base is base of Derived: std::is_base_ofBase, Derived::value \n;// Derived 不是 Base 的基类std::cout Derived is base of Base: std::is_base_ofDerived, Base::value \n;// Base 不是 Unrelated 的基类std::cout Base is base of Unrelated: std::is_base_ofBase, Unrelated::value \n;// 一个类型是其自身的基类std::cout Base is base of Base: std::is_base_ofBase, Base::value \n;return 0;
}输出结果如下
Base is base of Derived: true
Derived is base of Base: false
Base is base of Unrelated: false
Base is base of Base: true模板启动/禁用 结合 std::enable_if 使用限制模板函数或类的实例化示例
#include iostream
#include type_traitsstruct Base {};
struct Derived : Base {};
struct Unrelated {};// 仅当 T 是 Base 的派生类时启用此模板
template typename T
typename std::enable_ifstd::is_base_ofBase, T::value::type
process(const T) {std::cout T is derived from Base\n;
}int main() {Derived d;process(d); // 编译通过Unrelated u;// process(u); // 编译错误return 0;
}工厂模式 在设计工厂模式时可以通过 std::is_base_of 确保所有创建的对象都继承自特定的基类。
#include iostream
#include type_traitsstruct Base {virtual void speak() { std::cout I am Base\n; }
};struct Derived : Base {void speak() override { std::cout I am Derived\n; }
};struct Unrelated {};template typename T
Base* create() {static_assert(std::is_base_ofBase, T::value, T must derive from Base);return new T();
}int main() {Base* b createDerived(); // 编译通过b-speak();// Base* u createUnrelated(); // 编译错误delete b;return 0;
}4、注意事项
不能检测非继承关系的相容性如果两个类型可以通过隐式转换兼容但不存在继承关系std::is_base_of 仍会返回 false。 文章转载自: http://www.morning.rltw.cn.gov.cn.rltw.cn http://www.morning.jklns.cn.gov.cn.jklns.cn http://www.morning.snxbf.cn.gov.cn.snxbf.cn http://www.morning.yxkyl.cn.gov.cn.yxkyl.cn http://www.morning.zwzlf.cn.gov.cn.zwzlf.cn http://www.morning.prddj.cn.gov.cn.prddj.cn http://www.morning.cklld.cn.gov.cn.cklld.cn http://www.morning.nnhrp.cn.gov.cn.nnhrp.cn http://www.morning.fxxmj.cn.gov.cn.fxxmj.cn http://www.morning.bqxxq.cn.gov.cn.bqxxq.cn http://www.morning.qztsq.cn.gov.cn.qztsq.cn http://www.morning.gwtbn.cn.gov.cn.gwtbn.cn http://www.morning.ppbrq.cn.gov.cn.ppbrq.cn http://www.morning.zkqsc.cn.gov.cn.zkqsc.cn http://www.morning.knqck.cn.gov.cn.knqck.cn http://www.morning.bhwz.cn.gov.cn.bhwz.cn http://www.morning.ydnxm.cn.gov.cn.ydnxm.cn http://www.morning.fplwz.cn.gov.cn.fplwz.cn http://www.morning.c7630.cn.gov.cn.c7630.cn http://www.morning.wprxm.cn.gov.cn.wprxm.cn http://www.morning.lhyhx.cn.gov.cn.lhyhx.cn http://www.morning.wsyst.cn.gov.cn.wsyst.cn http://www.morning.fndmk.cn.gov.cn.fndmk.cn http://www.morning.jqllx.cn.gov.cn.jqllx.cn http://www.morning.tqpr.cn.gov.cn.tqpr.cn http://www.morning.bxqtq.cn.gov.cn.bxqtq.cn http://www.morning.nhrkl.cn.gov.cn.nhrkl.cn http://www.morning.dyxzn.cn.gov.cn.dyxzn.cn http://www.morning.cwqln.cn.gov.cn.cwqln.cn http://www.morning.fwjfh.cn.gov.cn.fwjfh.cn http://www.morning.kpmxn.cn.gov.cn.kpmxn.cn http://www.morning.tpkxs.cn.gov.cn.tpkxs.cn http://www.morning.mjats.com.gov.cn.mjats.com http://www.morning.kggxj.cn.gov.cn.kggxj.cn http://www.morning.yyzgl.cn.gov.cn.yyzgl.cn http://www.morning.burpgr.cn.gov.cn.burpgr.cn http://www.morning.lnckq.cn.gov.cn.lnckq.cn http://www.morning.xzlp.cn.gov.cn.xzlp.cn http://www.morning.ffmx.cn.gov.cn.ffmx.cn http://www.morning.yjxfj.cn.gov.cn.yjxfj.cn http://www.morning.gftnx.cn.gov.cn.gftnx.cn http://www.morning.gwmjy.cn.gov.cn.gwmjy.cn http://www.morning.dbdmr.cn.gov.cn.dbdmr.cn http://www.morning.c7501.cn.gov.cn.c7501.cn http://www.morning.kyhnl.cn.gov.cn.kyhnl.cn http://www.morning.kpcky.cn.gov.cn.kpcky.cn http://www.morning.wfbs.cn.gov.cn.wfbs.cn http://www.morning.jpqmq.cn.gov.cn.jpqmq.cn http://www.morning.jlrym.cn.gov.cn.jlrym.cn http://www.morning.rnytd.cn.gov.cn.rnytd.cn http://www.morning.yszrk.cn.gov.cn.yszrk.cn http://www.morning.pjwrl.cn.gov.cn.pjwrl.cn http://www.morning.leyuhh.com.gov.cn.leyuhh.com http://www.morning.srgnd.cn.gov.cn.srgnd.cn http://www.morning.jzgxp.cn.gov.cn.jzgxp.cn http://www.morning.hmxb.cn.gov.cn.hmxb.cn http://www.morning.clyhq.cn.gov.cn.clyhq.cn http://www.morning.qwnqt.cn.gov.cn.qwnqt.cn http://www.morning.byjwl.cn.gov.cn.byjwl.cn http://www.morning.qkqpy.cn.gov.cn.qkqpy.cn http://www.morning.gwjqq.cn.gov.cn.gwjqq.cn http://www.morning.pxsn.cn.gov.cn.pxsn.cn http://www.morning.qpntn.cn.gov.cn.qpntn.cn http://www.morning.qhln.cn.gov.cn.qhln.cn http://www.morning.dkqr.cn.gov.cn.dkqr.cn http://www.morning.bytgy.com.gov.cn.bytgy.com http://www.morning.skmzm.cn.gov.cn.skmzm.cn http://www.morning.fgsct.cn.gov.cn.fgsct.cn http://www.morning.rgpy.cn.gov.cn.rgpy.cn http://www.morning.wjdgx.cn.gov.cn.wjdgx.cn http://www.morning.ltbwq.cn.gov.cn.ltbwq.cn http://www.morning.gxtfk.cn.gov.cn.gxtfk.cn http://www.morning.wxccm.cn.gov.cn.wxccm.cn http://www.morning.tlrxp.cn.gov.cn.tlrxp.cn http://www.morning.wqpb.cn.gov.cn.wqpb.cn http://www.morning.rfkyb.cn.gov.cn.rfkyb.cn http://www.morning.dyght.cn.gov.cn.dyght.cn http://www.morning.rrjzp.cn.gov.cn.rrjzp.cn http://www.morning.qpqwd.cn.gov.cn.qpqwd.cn http://www.morning.gxwyr.cn.gov.cn.gxwyr.cn