C++ Reference ยท Memory and Utilities
C++ std::is_same, is_base_of and is_convertible
Reference for is_same, is_base_of, is_convertible and is_constructible: compare and relate types at compile time with static_assert and if constexpr.
These traits from <type_traits> answer questions about how two types relate. std::is_same<A, B> is true only if both names denote exactly the same type, including const and volatile qualifiers. std::is_base_of<Base, Derived> is true if Derived inherits from Base (directly or indirectly, publicly or not). std::is_convertible<From, To> is true if a value of From can be implicitly converted to To.
They are evaluated entirely at compile time, so their usual home is static_assert, if constexpr, std::enable_if and concept definitions. Each has a _v variable template that gives the bool directly.
static_assert(std::is_same_v<int, signed int>); static_assert(std::is_base_of_v<Animal, Dog>);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
template<class T, class U> struct is_same; // C++11 template<class Base, class Derived> struct is_base_of; // C++11 template<class From, class To> struct is_convertible; // C++11 template<class T, class... Args> struct is_constructible; template<class T, class U> inline constexpr bool is_same_v = is_same<T, U>::value;
| Name | Description |
|---|---|
T, U |
The two types to compare for is_same. |
Base, Derived |
Class types; Derived must be complete when Base and Derived are different non-union class types. |
From, To |
Source and target of an implicit conversion; To may be void. |
Args... |
Constructor argument types for is_constructible. |
Returns: value is true or false, usable in constant expressions.
Errors / undefined behavior: is_base_of with an incomplete Derived (and a different class type as Base) is undefined behavior and usually a compile error. Specializing these traits is undefined behavior.
Usage notes
is_sameis exact.intandconst int,intandint&, andcharandsigned charare all different. Usestd::remove_cvref_tfirst if you want to ignore qualifiers and references.- Aliases are transparent.
std::int32_tandintare the same type on common platforms because the alias names the same type;usingcreates no new type. is_base_ofignores access. It is true even for private or ambiguous bases, and it is true for a class and itself. It is false for non-class types such asint.is_convertibleis implicit only. Explicit constructors andexplicit operatordo not count. Useis_constructibleto ask whether a direct initialization works.- Narrowing conversions are still conversions.
doubletointis convertible; if you need to reject narrowing, build the check fromstd::is_arithmetic_vand ranges, or use a concept. - Pointer conversions:
Derived*is convertible toBase*whenBaseis an accessible, unambiguous base, which is the usual way to test an inheritance relationship through pointers. - Typical patterns:
static_assert(std::is_same_v<T, U>)to document assumptions;if constexpr (std::is_same_v<T, std::string>)to specialize behavior;requires std::is_base_of_v<Base, T>to constrain a template. - Concepts:
std::same_as,std::derived_fromandstd::convertible_to(<concepts>, C++20) are the concept versions.std::derived_fromadditionally requires public, unambiguous inheritance. - Related queries:
is_nothrow_convertible(C++20) andis_pointer_interconvertible_base_of(C++20) for special cases.