Skip to content
Tutorials Center
My learning

Topics

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.

C++
static_assert(std::is_same_v<int, signed int>);
static_assert(std::is_base_of_v<Animal, Dog>);

Try it

C++ Comparing and relating types

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.

C++ Convertible and constructible

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

C++
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_same is exact. int and const int, int and int&, and char and signed char are all different. Use std::remove_cvref_t first if you want to ignore qualifiers and references.
  • Aliases are transparent. std::int32_t and int are the same type on common platforms because the alias names the same type; using creates no new type.
  • is_base_of ignores 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 as int.
  • is_convertible is implicit only. Explicit constructors and explicit operator do not count. Use is_constructible to ask whether a direct initialization works.
  • Narrowing conversions are still conversions. double to int is convertible; if you need to reject narrowing, build the check from std::is_arithmetic_v and ranges, or use a concept.
  • Pointer conversions: Derived* is convertible to Base* when Base is 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_from and std::convertible_to (<concepts>, C++20) are the concept versions. std::derived_from additionally requires public, unambiguous inheritance.
  • Related queries: is_nothrow_convertible (C++20) and is_pointer_interconvertible_base_of (C++20) for special cases.