Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
josuttis_nm_c20_the_complete_guide.pdf
Скачиваний:
53
Добавлен:
27.03.2023
Размер:
5.85 Mб
Скачать

20.6 Afternotes

649

20.5.4 is_pointer_interconvertible_with_class()

template<typename S, typename M>

constexpr bool is_pointer_interconvertible_with_class(M S::*m) noexcept;

returns whether each object s of type S is pointer-interconvertible with its subobject s.*m. The function yields true if and only if S is a standard-layout type, M is an object type, and m is not null.

For example:

struct B1 { int x; }; struct B2 { int y; };

struct

DB1 :

B1 {};

struct

DB1B2

: B1, B2 {}; // not a standard-layout type

std::is_pointer_interconvertible_with_class<B1, int>(&DB1::x)

// true

std::is_pointer_interconvertible_with_class<DB1, int>(&B1::x)

// true

std::is_pointer_interconvertible_with_class<DB1, int>(&DB1::x)

// true

std::is_pointer_interconvertible_with_class<B1, int>(&DB1B2::x)

// true

std::is_pointer_interconvertible_with_class<DB1B2, int>(&B1::x)

// false

std::is_pointer_interconvertible_with_class<DB1B2, int>(&DB1B2::x)

// false

The C++20 standard explains the reason for the last two statements being false in a note:

The type of a pointer-to-member expression &C::b is not always a pointer to a member of C. This leads to potentially surprising results when using these functions in conjunction with inheritance:

struct A { int a; };

// a standard-layout class

struct B { int b; };

// a standard-layout class

struct C: public A, public B { };

// not a standard-layout class

std::is_pointer_interconvertible_with_class(&C::b)

// true

//true because, despite its appearance, &C::b has type

//“pointer to member of B of type int”

std::is_pointer_interconvertible_with_class<C>(&C::b) // false

//false because it forces the use of class C and fails

20.6Afternotes

The type traits is_bounded_array<> and is_unbounded_array<> were accepted as proposed by Walter E. Brown and Glen J. Fernandes in http://wg21.link/p1357r1.

The type trait is_nothrow_convertible<> was accepted as proposed by Daniel Krugler¨ in http: //wg21.link/p0758r1.

650

Chapter 20: New Type Traits

The type trait common _reference<> was accepted as part of the ranges library by Eric Niebler, Casey Carter, and Christopher Di Bella in http://wg21.link/p0896r4.

The type traits unwrap _reference<> and unwrap_ref_decay<> were accepted as proposed by Vicente J. Botet Escriba in http://wg21.link/p0318r1.

The type trait remove_cvref<> was accepted as proposed by Walter E. Brown in http://wg21.link/ p0550r2.

The type trait type_identity<> was accepted as proposed by Timur Doumler in http://wg21.link/ p0887r1.

The iterator type traits were accepted as part of adopting the ranges library as proposed by Eric Niebler, Casey Carter, and Christopher Di Bella in http://wg21.link/p0896r4.

The type traits is_layout_compatible<> and is_pointer_interconvertible_base_of<> as well as the functions is_corresponding_member() and is_pointer_interconvertible_with_class() were accepted as proposed by Lisa Lippincott in http://wg21.link/p0466r5.