std::derived_from
來自 cppreference.com
定義於標頭檔案 <concepts> |
||
template< class Derived, class Base > concept derived_from = |
(C++20 起) | |
概念 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 概念 `derived_from` [concept.derived]
- C++20 標準 (ISO/IEC 14882:2020)
- 18.4.3 概念 `derived_from` [concept.derived]
[編輯] 另請參閱
(C++11) |
檢查一個型別是否為另一個型別的基類 (類模板) |
(C++11)(C++20) |
檢查一個型別是否可以轉換為另一個型別 (類模板) |