名前空間
変種
操作

std::derived_from

From cppreference.com
< cpp‎ | concepts
 
 
 
ヘッダ <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以降)

concept derived_from<Derived, Base> は、`Base` が `Derived` であるか、または `Derived` の公開かつ一意な基底クラスである場合にのみ満たされます。cv修飾子は無視されます。

この動作は、`Base` が `Derived` のプライベートまたは保護された基底クラスである場合、std::is_base_of とは異なります。

[編集]

#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 Concept derived_from [concept.derived]
  • C++20 standard (ISO/IEC 14882:2020)
  • 18.4.3 Concept derived_from [concept.derived]

[編集] 関連項目

ある型が他の型の基底であるかをチェックする
(クラステンプレート) [編集]
ある型が他の型に変換可能であるかをチェックする
(クラステンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)