std::is_pointer_interconvertible_with_class
来自 cppreference.cn
定义于头文件 <type_traits> |
||
template< class S, class M > constexpr bool is_pointer_interconvertible_with_class( M S::* mp ) noexcept; |
(自 C++20 起) | |
给定类型 S
的对象 s
,确定 s.*mp 是否引用 s
的子对象,且 s
是否与其子对象 s.*mp 指针可互转换。若 S
不是完整类型,则程序为非良构。
如果 S
不是 StandardLayoutType,或 M
不是对象类型,或 mp
等于 nullptr,则结果始终为 false。
目录 |
[编辑] 参数
mp | - | 要检测的指向成员的指针 |
[编辑] 返回值
true 若 s.*mp 指代 s
的子对象且 s
与其子对象 s.*mp 指针可互转换,否则为 false,其中 s
是类型 S
的对象。
[编辑] 注解
指向成员的指针表达式 &S::m 的类型不总是 M S::*,其中 m
的类型为 M
,因为 m
可能是从 S
的基类继承的成员。可以指定模板实参以避免潜在的意外结果。
若存在类型为 M S::* 的值 mp
使得 std::is_pointer_interconvertible_with_class(mp) == true,则 reinterpret_cast<M&>(s) 具有良好定义的结果,并且它指代与 s.*mp 相同的子对象,其中 s
是类型 S
的有效左值。
在常见平台上,如果 std::is_pointer_interconvertible_with_class(mp) == true,则 mp
的位模式全为零。
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_is_pointer_interconvertible |
201907L |
(C++20) | 指针可互转换性特征
|
[编辑] 示例
运行此代码
#include <type_traits> struct Foo { int x; }; struct Bar { int y; }; struct Baz : Foo, Bar {}; // not standard-layout static_assert( not std::is_same_v<decltype(&Baz::x), int Baz::*> ); static_assert( std::is_pointer_interconvertible_with_class(&Baz::x) ); static_assert( not std::is_pointer_interconvertible_with_class<Baz, int>(&Baz::x) ); int main() { }
[编辑] 参见
(C++11) |
检查类型是否为标准布局类型 (类模板) |
(C++11) |
检查类型是否为非静态成员对象指针 (类模板) |