C++ cast备忘

  1. 这格式见了鬼了,表格出不来,懒得找原因了。
  2. 从boost-1.53.0开始,shared_polymorphic_cast和shared_polymorphic_downcast被删除,分别变为polymorphic_pointer_cast和polymorphic_pointer_downcast。(没有向前兼容这种事情实在……)
名称 介绍
upcast 从派生到基类
downcast 从基类到派生
名称 介绍
dynamic_cast upcast and downcast with target type check
static_cast upcast and downcast without target type check
reinterpret_cast all kinds cast without target type check
boost::static_pointer_cast 作用范围同static_cast,用于智能指针的类型转换,同时处理引用计数
boost::dynamic_pointer_cast 作用范围同dynamic_cast,用于智能指针的类型转换,同时处理引用计数
boost::reinterpret_pointer_cast 作用范围同reinterpret_cast,用于智能指针的类型转换,同时处理引用计数
boost::shared_polymorphic_downcast/boost::polymorphic_pointer_downcast 作用范围同static_cast,在定义NDEBUG时,使用dynamic做运行时检测,未定义NDEBUG时,同static_cast,用于智能指针的类型转换,同时处理引用计数
boost::shared_polymorphic_cast/boost::polymorphic_pointer_cast: 作用范围同dynamic_cast,类型转换失败时抛出异常,用于智能指针的类型转换,同时处理引用计数
boost::polymorphic_cast 作用范围同dynamic_cast,类型转换失败时抛出异常,用于非智能指针的类型转换
boost::polymorphic_downcast 作用范围同static_cast,未定义NDEBUG时,同static_cast,用于智能指针的类型转换,同时处理引用计数,用于非智能指针的类型转换