Namespaces
Variants

std:: is_corresponding_member

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 S1, class S2, class M1, class M2 >
constexpr bool is_corresponding_member ( M1 S1 :: * mp, M2 S2 :: * mq ) noexcept ;
(C++20 起)

确定 mp mq 是否指向 S1 S2 公共起始序列 中的对应成员。若 S1 S2 不完整类型 ,则程序非良构。

如果 S1 S2 不是 标准布局类型 ,或者 M1 M2 不是对象类型,或者 mp mq 等于 nullptr ,则结果始终为 false

目录

参数

mp, mq - 待检测的成员指针

返回值

如果 mp mq 指向 S1 S2 公共初始序列中的对应成员,则为 true ;否则为 false

注释

指向成员表达式 & S :: m 的类型并不总是 M S :: * ,因为 m 可能是从 S 的基类继承而来的成员。可以通过显式指定模板参数来避免可能出现的意外结果。

示例

#include <type_traits>
struct Foo
{
    int x;
    double d;
};
struct Bar
{
    int y;
    double z;
};
struct Baz : Foo, Bar {}; // 非标准布局
static_assert(
    std::is_same_v<decltype(&Baz::x), int Foo::*> == true &&
    std::is_same_v<decltype(&Baz::y), int Bar::*> == true &&
    std::is_corresponding_member(&Foo::x, &Bar::y) == true &&
    std::is_corresponding_member(&Foo::d, &Bar::z) == true &&
    std::is_corresponding_member(&Baz::x, &Baz::y) == true &&
    std::is_corresponding_member<Baz, Baz, int, int>(&Baz::x, &Baz::y) == false
);
int main() {}

参见

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