std::conditional (C++)

First used this at Tesla when working on floating point numbers, to determine the storage type based on the size of the input.

Resources

using Type2 = std::conditional<false, int, double>::type;

Implementation

First idea (INCORRECT)

template <bool Condition, typename T, typename U> 
struct conditional {
    using type = Condition : typename T ? typename U; // this doesn't work, you must use the template way
}

No, similar to std::is_same, you make use of template specialization

template <bool Condition, typename T, typename U> 
class conditional {
    using type = typename T;
}
 
 
template <typename T, typename U>
class conditional<true, T, U> {
    using type = typename U;
}

Difference with std::enable_if?

std::conditional Always results in a valid type, either TypeIfTrue or TypeIfFalse, whereas std::enable_if will only result in a valid type if the condition evaluates to true.