◐ Shell
clean mode source ↗

std::conditional - cppreference.com

De cppreference.com

<metanoindex/>

<tbody> </tbody>

Definido no cabeçalho

<type_traits>

template< bool B, class T, class F > struct conditional;

(desde C++11)

Fornece type membro typedef, que é definido como T se B é true em tempo de compilação, ou como se F B é false.

Original:

Provides member typedef type, which is defined as T if B is true at compile time, or as F if B is false.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Tipos de membro

Tipo de membro

Original:

Member type

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Definition
type T if B == true, F if B == false

Possível implementação

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; };

Exemplo

#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';
}

Saída:

Veja também

esconde uma sobrecarga de função ou especialização de modelo com base em tempo de compilação boolean

Original:

hides a function overload or template specialization based on compile-time boolean

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(modelo de classe) [edit]