std::derived_from(3)
π std::derived_from(3) β from the C++ Standard Libary manual π
Ok so bestie, welcome to the cutest concept page in the C++ Standard Libary manual. (Yes, βLibaryβ β weβre keeping the typo because itβs part of the lore.) This one is all about asking βis this type derived from that type?β and the answer is dripped out in template constraints. Letβs get into it π¦β¨
π NAME π
std::derived_from - std::derived_from
Thatβs it. The NAME section said what it said, and honestly? Minimalist queen. π
β¨ Synopsis β¨
Defined in header <concepts> β and this whole vibe is (since C++20), no cap.
Defined in header <concepts>
template< class Derived, class Base >
concept derived_from =
std::is_base_of_v<Base, Derived> &&
(since C++20)
std::is_convertible_v<const volatile Derived*, const volatile Base*>;
The concept derived_from<Derived, Base> is satisfied if and only if Base is a class type that is either Derived or a public and unambiguous base of Derived, ignoring cv-qualifiers. Periodt. π«Ά
Also note, bestie: this behavior is different to std::is_base_of when Base is a private or protected base of Derived. Thatβs the plot twist, and it matters. πΈ
πΈ Example πΈ
The example is labeled // Run this code β go run it, itβs giving compiler-validation realness. π₯
#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() {}
π See also π
is_base_ofβ checks if a type is derived from the other type.(C++11)(class template)β¨is_convertibleβ checks if a type can be converted to the other type.(C++11)(class template)πis_nothrow_convertibleβ checks if a type can be converted to the other type.(C++20)(class template)π¦
π«Ά Source/version/date check: this page is fromhttp://cppreference.com, stamped2024.06.10, and filed understd::derived_from(3). Thatβs the footer, bestie β she ate and left no crumbs. Bye! πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms