std::is_corresponding_member
来自cppreference.com
在标头 <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
不是标准布局类型 (StandardLayoutType) ,或若 M1
或 M2
不是对象类型,或若 mp 或 mq 等于 nullptr,则结果始终为 false。
参数
mp, mq | - | 要检测的成员指针 |
返回值
若 mp 与 mq 指代 S1
与 S2
的公共起始序列中的对应成员则为 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() {}
参阅
(C++11) | 检查类型是否为标准布局类型 (类模板) |
(C++20) | 检查两个类型是否布局兼容 (类模板) |
(C++11) | 检查类型是否为指向非静态成员对象的指针 (类模板) |