Namespaces
Variants
Views
Actions

std::is_pointer_interconvertible_with_class

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
(C++11)
(C++11)
is_pointer_interconvertible_with_class
(C++20)
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 S, class M>
constexpr bool is_pointer_interconvertible_with_class( M S::* mp ) noexcept;
(since C++20)

Given an object s of type S, determines whether s.*mp refers to a subobject of s and s is pointer-interconvertible with its subobject s.*mp. The program is ill-formed if S is not a complete type.

If S is not a StandardLayoutType, or M is not an object type, or mp is equal to nullptr, the result is always false.

Contents

[edit] Parameters

mp - a pointer-to-member to detect

[edit] Return value

true if s.*mp refers a subobject of s and s is pointer-interconvertible with its subobject s.*mp, otherwise false, where s is an object of type S.

[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 template argument can be specified in order to avoid potentially surprising results.

If there is a value mp of type M S::* such that std::is_pointer_interconvertible_with_class(mp) == true, then reinterpret_cast<M&>(s) has well-defined result and it refers the same subobject as s.*mp, where s is a valid lvalue of type S.

On common platforms, the bit pattern of mp is all zero if std::is_pointer_interconvertible_with_class(mp) == true.

[edit] Example

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

Output:

false
true
false

[edit] See also

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