std::derived_from
From cppreference.com
| ヘッダ <concepts> で定義 |
||
| template< class Derived, class Base > concept derived_from = |
(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]
- 18.4.3 Concept
- C++20 standard (ISO/IEC 14882:2020)
- 18.4.3 Concept
derived_from[concept.derived]
- 18.4.3 Concept
[編集] 関連項目
| (C++11) |
ある型が他の型の基底であるかをチェックする (クラステンプレート) |
| (C++11)(C++20) |
ある型が他の型に変換可能であるかをチェックする (クラステンプレート) |