Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::conditional

      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
      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)

      conditional
      (C++11)
      (C++11)
      (C++17)
      Compile-time rational arithmetic
      Compile-time integer sequences
       
      Defined in header<type_traits>
      template<bool B,class T,class F>
      struct conditional;
      (since C++11)

      Provides member typedeftype, which is defined asT ifB istrue at compile time, or asF ifB isfalse.

      If the program adds specializations forstd::conditional, the behavior is undefined.

      Contents

      [edit]Member types

      Member type Definition
      typeT ifB==true,F ifB==false

      [edit]Helper types

      template<bool B,class T,class F>
      using conditional_t=typename conditional<B,T,F>::type;
      (since C++14)

      [edit]Possible implementation

      template<bool B,class T,class F>struct conditional{using type= T;}; template<class T,class F>struct conditional<false, T, F>{using type= F;};

      [edit]Example

      Run this code
      #include <iostream>#include <type_traits>#include <typeinfo> int main(){using Type1= std::conditional<true,int,double>::type;using Type2= std::conditional<false,int,double>::type;using Type3= std::conditional<sizeof(int)>= sizeof(double),int,double>::type; std::cout<<typeid(Type1).name()<<'\n';std::cout<<typeid(Type2).name()<<'\n';std::cout<<typeid(Type3).name()<<'\n';}

      Possible output:

      intdoubledouble

      [edit]See also

      (C++11)
      conditionallyremoves a function overload or template specialization from overload resolution
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/types/conditional&oldid=152806"

      [8]ページ先頭

      ©2009-2025 Movatter.jp