Spec-Zone.ru › C++

std::derived_from

Определён в заголовочном файле <concepts>
template< class Derived, class Base >
concept derived_from =
    std::is_base_of_v<Base, Derived> &&
    std::is_convertible_v<const volatile Derived*, const volatile Base*>;
(начиная с C++20)

Концепт derived_from<Derived, Base> удовлетворяется тогда и только тогда, когда Base является типом класса, который либо Derived, либо является его открытым и однозначным базовым классом Derived; квалификаторы cv игнорируются.

Обратите внимание, что это поведение отличается от std::is_base_of, если Base является закрытым или защищённым базовым классом Derived.

Пример

#include <concepts>

class A {};

class B : public A {};

class C : private A {};

// std::derived_from == true only for public inheritance or exact same class
static_assert(std::derived_from<B, B> == true);      // same class: true
static_assert(std::derived_from<int, int> == false); // same primitive type: false
static_assert(std::derived_from<B, A> == true);      // public inheritance: true
static_assert(std::derived_from<C, A> == false);     // private inheritance: false

// std::is_base_of == true also for private inheritance
static_assert(std::is_base_of_v<B, B> == true);      // same class: true
static_assert(std::is_base_of_v<int, int> == false); // same primitive type: false
static_assert(std::is_base_of_v<A, B> == true);      // public inheritance: true
static_assert(std::is_base_of_v<A, C> == true);      // private inheritance: true

int main() {}

Ссылки

  • Стандарт C++23 (ISO/IEC 14882:2024):
    • 18.4.3 Концепт derived_from [concept.derived]
  • Стандарт C++20 (ISO/IEC 14882:2020):
    • 18.4.3 Концепт derived_from [concept.derived]

См. также

is_base_of
(C++11)
проверяет, является ли один тип базовым для другого типа
(шаблон класса)
is_convertibleis_nothrow_convertible
(C++11)(C++20)
проверяет, можно ли преобразовать один тип в другой
(шаблон класса)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/concepts/derived_from

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API