Namespaces
Variants
Views
Actions

std::is_corresponding_member

From cppreference.com
< cpp‎ | types
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

Elementary string conversions
(C++17)
(C++17)
 
Type support
Basic types
Fundamental types
Fixed width integer types (C++11)
Numeric limits
C numeric limits interface
Runtime type information
Type traits
Type categories
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(until C++20)
(C++11)(deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Constant evaluation context
Supported operations
Relationships and property queries
Type modifications
(C++11)(C++11)(C++11)
Type transformations
(C++11)
(C++11)
(C++17)
(C++11)(until C++20)(C++17)
 
Defined in header <type_traits>
template<class S1, class S2, class M1, class M2>
constexpr bool is_corresponding_member( M1 S1::* mp, M2 S2::* mq ) noexcept;
(since C++20)

Determines whether mp and mq refer corresponding members in the common initial sequence of S1 and S2. The program is ill-formed if either S1 or S2 is an incomplete type.

If either S1 or S2 is not a StandardLayoutType, or either M1 or M2 is not an object type, or either mp or mq is equal to nullptr, the result is always false.

Contents

[edit] Parameters

mp, mq - pointers-to-member to detect

[edit] Return value

true if mp and mq refer corresponding members in the common initial sequence of S1 and S2, otherwise false.

[edit] Notes

The type of a pointer-to-member expression &S::m is not always M S::*, where m is of type M, because m may be a member inherited from a base class of S. The first two template arguments can be specified in order to avoid potentially surprising results.

[edit] Example

#include <type_traits>
#include <iostream>
 
struct Foo { int x; };
struct Bar { int y; double z; };
 
struct Baz : Foo, Bar {}; // not standard-layout
 
int main()
{
    std::cout << std::boolalpha
        << std::is_same_v<decltype(&Baz::x), int Foo::*> << '\n'
        << std::is_same_v<decltype(&Baz::y), int Bar::*> << '\n'
        << std::is_corresponding_member(&Foo::x, &Bar::y) << '\n'
        << std::is_corresponding_member(&Baz::x, &Baz::y) << '\n'
        << std::is_corresponding_member<Baz, Baz>(&Baz::x, &Baz::y) << '\n';
}

Output:

true
true
true
true
false

[edit] See also

checks if a type is a standard-layout type
(class template) [edit]
checks if two types are layout-compatible
(class template) [edit]
checks if a type is a pointer to a non-static member object
(class template) [edit]