std::is_pointer_interconvertible_base_of

来自cppreference.com
< cpp‎ | types
 
 
元编程库
类型特征
类型类别
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
类型属性
(C++11)
(C++11)
(C++14)
(C++11)(C++26 弃用)
(C++11)(C++20 前*)
(C++11)(C++20 弃用)
(C++11)
类型特征常量
元函数
(C++17)
受支持操作
关系与属性查询
类型修改
类型变换
(C++11)(C++23 弃用)
(C++11)(C++23 弃用)
(C++11)
(C++11)(C++20 前*)(C++17)

编译时有理数算术
编译时整数序列
 
在标头 <type_traits> 定义
template< class Base, class Derived >
struct is_pointer_interconvertible_base_of;
(C++20 起)

如果 Derived 无歧义地派生自 Base 且每个 Derived 对象均与其 Base 子对象指针可以互相转换,或它们均为相同的非联合类类型(两种情况下都忽略 cv 限定),那么提供的成员常量 value 等于 true。否则,value 等于 false

如果 BaseDerived 均为非联合类类型且不是同一类型(忽略 cv 限定),那么 Derived 应当为完整类型;否则行为未定义。

如果程序添加了 std::is_pointer_interconvertible_base_ofstd::is_pointer_interconvertible_base_of_v 的特化,那么行为未定义。

辅助变量模板

template< class Base, class Derived >

inline constexpr bool is_pointer_interconvertible_base_of_v =

    is_pointer_interconvertible_base_of<Base, Derived>::value;
(C++20 起)

继承自 std::integral_constant

成员常量

value
[静态]
如果 Derived 无歧义地派生自 Base 且每个 Derived 对象均与其 Base 子对象指针可以互相转换,或它们均为相同的非联合类类型(两种情况下都忽略 cv 限定)那么是 true,否则是 false
(公开静态成员常量)

成员函数

operator bool
将对象转换到 bool,返回 value
(公开成员函数)
operator()
(C++14)
返回 value
(公开成员函数)

成员类型

类型定义
value_typebool
typestd::integral_constant<bool, value>

注解

即使 TU 的私有或受保护基类,std::is_pointer_interconvertible_base_of_v<T, U> 亦可以为 true

  • U 为完整对象类型,
  • T 为 cv 限定不少于 U 的完整对象类型,
  • u 为任何合法的 U 左值,

std::is_pointer_interconvertible_base_of_v<T, U>true,则 reinterpret_cast<T&>(u) 始终有良定义的结果。

TU 不是同一类型(忽略 cv 限定)且 TU 的指针可以互相转换基类,则 std::is_standard_layout_v<T>std::is_standard_layout_v<U> 均为 true

T 为标准布局类类型,则 T 的所有基类(若存在)均为 T 的指针可以互相转换基类。

功能特性测试标准功能特性
__cpp_lib_is_pointer_interconvertible201907L(C++20)指针可以互相换性特征:
  • std::is_pointer_interconvertible_base_of
  • std::is_pointer_interconvertible_with_class

示例

#include <type_traits>
 
struct Foo {};
 
struct Bar {};
 
class Baz : Foo, public Bar { int x; };
 
class NonStdLayout : public Baz { int y; };
 
static_assert(std::is_pointer_interconvertible_base_of_v<Bar, Baz>);
static_assert(std::is_pointer_interconvertible_base_of_v<Foo, Baz>);
static_assert(not std::is_pointer_interconvertible_base_of_v<Baz, NonStdLayout>);
static_assert(std::is_pointer_interconvertible_base_of_v<NonStdLayout, NonStdLayout>);
 
int main() {}

参阅

检查一个类型是否为另一个类型的基类
(类模板)
(C++11)
检查类型是否为类(但非联合体)类型且无非静态数据成员
(类模板)
检查类型是否为标准布局类型
(类模板)