std::is_corresponding_member

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

编译时有理数算术
编译时整数序列
 
在标头 <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 起)

确定 mpmq 是否指代 S1S2公共起始序列中的对应成员。若 S1S2不完整类型则程序非良构。

S1S2 不是标准布局类型 (StandardLayoutType) ,或若 M1M2 不是对象类型,或若 mpmq 等于 nullptr,则结果始终为 false

参数

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

返回值

mpmq 指代 S1S2 的公共起始序列中的对应成员则为 true,否则为 false

注解

成员指针表达式 &S::m 的类型并非始终是 M S::*,其中 m 的类型为 M,因为 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() {}

参阅

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