std::is_pointer_interconvertible_base_of
来自 cppreference.cn
定义于头文件 <type_traits> |
||
template< class Base, class Derived > struct is_pointer_interconvertible_base_of; |
(自 C++20 起) | |
如果 Derived
从 Base
明确派生,并且每个 Derived
对象都与其 Base
子对象指针可互转换,或者如果两者是相同的非联合类(在这两种情况下都忽略 cv 限定),则提供等于 true 的成员常量 value
。否则 value
为 false。
如果 Base
和 Derived
都是非联合类类型,并且它们不是相同的类型(忽略 cv 限定),则 Derived
应当是完整类型;否则行为未定义。
如果程序为 std::is_pointer_interconvertible_base_of
或 std::is_pointer_interconvertible_base_of_v
添加特化,则行为未定义。
内容 |
[编辑] 辅助变量模板
template< class Base, class Derived > inline constexpr bool is_pointer_interconvertible_base_of_v = |
(自 C++20 起) | |
继承自 std::integral_constant
成员常量
value [静态] |
如果 Derived 从 Base 明确派生,并且每个 Derived 对象都与其 Base 子对象指针可互转换,或者如果两者是相同的非联合类(在这两种情况下都忽略 cv 限定),则为 true,否则为 false(公有静态成员常量) |
成员函数
operator bool |
将对象转换为 bool,返回 value (公有成员函数) |
operator() (C++14) |
返回 value (公有成员函数) |
成员类型
类型 | 定义 |
value_type
|
bool |
type
|
std::integral_constant<bool, value> |
[编辑] 注解
std::is_pointer_interconvertible_base_of_v<T, U> 即使 T
是 U
的私有或受保护基类,也可能为 true。
设
-
U
为完整对象类型, -
T
为完整对象类型,其 cv 限定不低于U
, -
u
为U
的任何有效左值,
reinterpret_cast<T&>(u) 如果 std::is_pointer_interconvertible_base_of_v<T, U> 为 true,则始终具有良好定义的结果。
如果 T
和 U
不是相同的类型(忽略 cv 限定)且 T
是 U
的指针可互转换基类,则 std::is_standard_layout_v<T> 和 std::is_standard_layout_v<U> 均为 true。
如果 T
是标准布局类类型,则 T
的所有基类(如果有)都是 T
的指针可互转换基类。
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_is_pointer_interconvertible |
201907L |
(C++20) | 指针可互换性特性
|
[编辑] 示例
运行此代码
#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) |
检查一个类型是否是另一个类型的基类 (类模板) |
(C++11) |
检查一个类型是否是类(但不是联合)类型且没有非静态数据成员 (类模板) |
(C++11) |
检查一个类型是否是 标准布局 类型 (类模板) |