Namespaces
Variants

std:: is_pointer_interconvertible_with_class

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
定义于头文件 <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 不是 标准布局类型 ,或 M 不是对象类型,或 mp 等于 nullptr ,则结果始终为 false

目录

参数

mp - 待检测的成员指针

返回值

s. * mp 指向 s 的子对象且 s 与其子对象 s. * mp 满足指针可互转换条件时返回 true ,否则返回 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 的有效左值。

在常见平台上,若 mp 的位模式全为零,则 std :: is_pointer_interconvertible_with_class ( mp ) == true

功能测试 标准 功能特性
__cpp_lib_is_pointer_interconvertible 201907L (C++20) 指针可互转换特性:

示例

#include <type_traits>
struct Foo { int x; };
struct Bar { int y; };
struct Baz : Foo, Bar {}; // 非标准布局类型
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() { }

参见

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