std::conditional
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <type_traits> で定義
|
||
template< bool B, class T, class F > struct conditional; |
(C++11以上) | |
コンパイル時に B が true であれば T として、 B が false であれば F として定義されるメンバ型 type を提供します。
メンバ型
| メンバ型 | 定義 |
type
|
B == true であれば T、 B == false であれば F
|
ヘルパー型
<tbody> </tbody> template< bool B, class T, class F > using conditional_t = typename conditional<B,T,F>::type; |
(C++14以上) | |
実装例
template<bool B, class T, class F>
struct conditional { typedef T type; };
template<class T, class F>
struct conditional<false, T, F> { typedef F type; };
|
例
Run this code
#include <iostream>
#include <type_traits>
#include <typeinfo>
int main()
{
typedef std::conditional<true, int, double>::type Type1;
typedef std::conditional<false, int, double>::type Type2;
typedef std::conditional<sizeof(int) >= sizeof(double), int, double>::type Type3;
std::cout << typeid(Type1).name() << '\n';
std::cout << typeid(Type2).name() << '\n';
std::cout << typeid(Type3).name() << '\n';
}
出力例:
int
double
double
関連項目
(C++11) |
コンパイル時ブーリアンに基づいて関数オーバーロードまたはテンプレート特殊化を隠蔽します (クラステンプレート) |