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 のいずれかが StandardLayoutType でない場合、あるいは M1 または M2 のいずれかがオブジェクト型でない場合、あるいは mp または mq のいずれかが nullptr と等しい場合、結果は常に false となります。

目次

パラメータ

mp, mq - 検出するメンバへのポインタ

戻り値

true が返されるのは、 mp mq S1 S2 の共通先頭シーケンス内の対応するメンバを参照している場合であり、それ以外の場合は 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 {}; // not standard-layout
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() {}

関連項目

型が standard-layout 型であるかどうかをチェックする
(クラステンプレート)
2つの型が layout-compatible であるかどうかをチェックする
(クラステンプレート)
型が非静的メンバオブジェクトポインタであるかどうかをチェックする
(クラステンプレート)