命名空间
变体
操作

std::is_pointer_interconvertible_with_class

来自 cppreference.cn
< cpp‎ | types
 
 
元编程库
类型特征
类型类别
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(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++11)(C++11)
类型转换
(C++11)(C++23 中已弃用)
(C++11)(C++23 中已弃用)
(C++11)
(C++11)(直到 C++20*)(C++17)

(C++11)
(C++17)
编译时有理算术
编译时整数序列
 
定义于头文件 <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 - 要检测的指向成员的指针

[编辑] 返回值

trues.*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() { }

[编辑] 参见

检查类型是否为标准布局类型
(类模板) [编辑]
检查类型是否为非静态成员对象指针
(类模板) [编辑]