You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
this is useful when using unsigned and signed types
tl::optional<size_t> x = get_PossibleVal();
example 1
// equivalent to: float a = x.hasValue() ? *x : static_cast<size_t>(NAN);// ERROR: size_t is unsigned type and will convert to size_t 0// ERROR: resulting float is [value or 0] instead of [value or NAN]float a = x.value_or<float>(NAN);
// equivalent to: float b = x.hasValue() ? static_cast<float>(*x) : NAN;// resulting float is value or NANfloat b = x.value_or_cast<float>(NAN);
example 2
// equivalent to: float a = x.hasValue() ? *x : static_cast<size_t>(-1);// ERROR: size_t is unsigned type and will convert to size_t 18446744073709551615// ERROR: resulting float is [value or 1.8446744073709551615] instead of [value or -1]float a = x.value_or<float>(-1);
// equivalent to: float b = x.hasValue() ? static_cast<float>(*x) : -1;// resulting float is value or -1float b = x.value_or_cast<float>(-1);
new code
/// Returns the stored value if there is one, otherwise returns `u`template <classU> constexpr U value_or_cast(U &&u) const & {
static_assert(std::is_copy_constructible<U>::value &&
std::is_convertible<T, U>::value,
"U must be copy constructible and convertible from T");
returnhas_value() ? static_cast<U>(**this) : std::forward<U>(u);
}
/// Returns the stored value if there is one, otherwise returns `u`template <classU> TL_OPTIONAL_11_CONSTEXPR U value_or_cast(U &&u) && {
static_assert(std::is_move_constructible<U>::value &&
std::is_convertible<T, U>::value,
"U must be move constructible and convertible from T");
returnhas_value() ? static_cast<U>(**this) : std::forward<U>(u);
}
/// Returns the stored value if there is one, otherwise returns `u`template <classU> constexpr U value_or_cast(U &&u) const & noexcept {
static_assert(std::is_copy_constructible<U>::value &&
std::is_convertible<T, U>::value,
"U must be copy constructible and convertible from T");
returnhas_value() ? static_cast<U>(**this) : std::forward<U>(u);
}
/// \group value_or_casttemplate <classU> TL_OPTIONAL_11_CONSTEXPR U value_or_cast(U &&u) && noexcept {
static_assert(std::is_move_constructible<U>::value &&
std::is_convertible<T, U>::value,
"U must be move constructible and convertible from T");
returnhas_value() ? static_cast<U>(**this) : std::forward<U>(u);
}
The text was updated successfully, but these errors were encountered:
add support for
value_or_cast
this is useful when using unsigned and signed types
tl::optional<size_t> x = get_PossibleVal();
example 1
example 2
new code
The text was updated successfully, but these errors were encountered: