Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::is_base_of

      From cppreference.com
      <cpp‎ |types
       
       
      Metaprogramming library
      Type traits
      Type categories
      (C++11)
      (C++11)(DR*)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11) 
      Type properties
      (C++11)
      (C++11)
      (C++14)
      (C++11)(deprecated in C++26)
      (C++11)(until C++20*)
      (C++11)(deprecated in C++20)
      (C++11)
      Type trait constants
      Metafunctions
      (C++17)
      Supported operations
      Relationships and property queries
      is_base_of
      (C++11)
      (C++11)
      (C++11)
      Type modifications
      Type transformations
      (C++11)(deprecated in C++23)
      (C++11)(deprecated in C++23)
      (C++11)
      (C++11)(until C++20*)(C++17)

      Compile-time rational arithmetic
      Compile-time integer sequences
       
      Defined in header<type_traits>
      template<class Base,class Derived>
      struct is_base_of;
      (since C++11)

      std::is_base_of is aBinaryTypeTrait.

      IfDerived isderived fromBase or if both are the same non-union class (in both cases ignoring cv-qualification), provides the member constantvalue equal totrue. Otherwisevalue isfalse.

      If bothBase andDerived are non-union class types, and they are not the same type (ignoring cv-qualification),Derived should be acomplete type; otherwise the behavior is undefined.

      If the program adds specializations forstd::is_base_of orstd::is_base_of_v(since C++17), the behavior is undefined.

      Contents

      [edit]Helper variable template

      template<class Base,class Derived>
      constexprbool is_base_of_v= is_base_of<Base, Derived>::value;
      (since C++17)
      [edit]

      Inherited fromstd::integral_constant

      Member constants

      value
      [static]
      true ifDerived is derived fromBase or if both are the same non-union class (in both cases ignoring cv-qualification),false otherwise
      (public static member constant)

      Member functions

      operator bool
      converts the object tobool, returnsvalue
      (public member function)
      operator()
      (C++14)
      returnsvalue
      (public member function)

      Member types

      Type Definition
      value_typebool
      typestd::integral_constant<bool, value>

      [edit]Notes

      std::is_base_of<A, B>::value istrue even ifA is a private, protected, or ambiguous base class ofB. In many situations,std::is_convertible<B*, A*> is the more appropriate test.

      Although no class is its own base,std::is_base_of<T, T>::value is true because the intent of the trait is to model the "is-a" relationship, andT is aT. Despite that,std::is_base_of<int,int>::value isfalse because only classes participate in the relationship that this trait models.

      [edit]Possible Implementation

      namespace details{template<typename B>std::true_type test_ptr_conv(constvolatile B*);template<typename>std::false_type test_ptr_conv(constvolatilevoid*); template<typename B,typename D>auto test_is_base_of(int)-> decltype(test_ptr_conv<B>(static_cast<D*>(nullptr)));template<typename,typename>auto test_is_base_of(...)->std::true_type;// private or ambiguous base} template<typename Base,typename Derived>struct is_base_of:std::integral_constant<bool,std::is_class<Base>::value&&std::is_class<Derived>::value&&        decltype(details::test_is_base_of<Base, Derived>(0))::value>{};

      [edit]Example

      Run this code
      #include <type_traits> class A{};class B: A{};class C: B{};class D{};union E{};using I=int; static_assert(    std::is_base_of_v<A, A>==true&&    std::is_base_of_v<A, B>==true&&    std::is_base_of_v<A, C>==true&&    std::is_base_of_v<A, D>!=true&&    std::is_base_of_v<B, A>!=true&&    std::is_base_of_v<E, E>!=true&&    std::is_base_of_v<I, I>!=true); int main(){}

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2015C++11the behavior might be undefined if
      Derived is an incomplete union type
      the base characteristic is
      std::false_type in this case

      [edit]See also

      checks if a type is a virtual base of the other type
      (class template)[edit]
      checks if a type can be converted to the other type
      (class template)[edit]
      specifies that a type is derived from another type
      (concept)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/types/is_base_of&oldid=176479"

      [8]ページ先頭

      ©2009-2025 Movatter.jp