Type relationship safely_constructible (original) (raw)

The safely_constructible type trait is a refinement of std::is_constructible, which has special behavior for fundamental types and pointer types and forbids certain conversions.

Valid Expressions

expression value
T, U any types
safely_constructible<T, U>::value true if a variant for which T is one of the value types should be constructible from U.

Notes

Examples

static_assert(safely_constructible<unsigned char, char>::value, ""); static_assert(!safely_constructible<char, unsigned char>::value, "");

static_assert(safely_constructible<unsigned int, int>::value, ""); static_assert(!safely_constructible<int, char>::value, ""); static_assert(!safely_constructible<int, bool>::value, ""); static_assert(!safely_constructible<char, bool>::value, "");

static_assert(safely_constructible<double, float>::value, ""); static_assert(!safely_constructible<float, double>::value, ""); static_assert(!safely_constructible<int, double>::value, ""); static_assert(!safely_constructible<double, int>::value, "");

static_assert(!safely_constructible<bool, const char *>::value, ""); static_assert(!safely_constructible<const void *, const char *>::value, "");

static_assert(safely_constructible<const char *, char *>::value, ""); static_assert(!safely_constructible<char *, const char *>::value, "");

static_assert(safely_constructible<const char *, decltype("foo")>::value, ""); static_assert(!safely_constructible<char *, decltype("foo")>::value, "");

static_assert(safely_constructible<std::string, const char *>::value, ""); static_assert(safely_constructible<std::string, decltype("foo")>::value, ""); static_assert(!safely_constructible<const char *, std::string>::value, "");

[Note] Note
You may specialize safely_constructible as you please in order to modify the behavior of strict_variant.

Definition

The complete definition of the safely_constructible template is given:

template struct decay_to_arithmetic : std::is_arithmetic<mpl::decay_t> {};

template struct decay_to_ptr : std::is_pointer<mpl::decay_t> {};

template <typename A, typename B, typename ENABLE = void> struct safely_constructible : std::is_constructible<A, B> {};

template <typename A, typename B> struct safely_constructible<A, B, mpl::enable_if_t<decay_to_arithmetic::value && decay_to_arithmetic::value>> : safe_arithmetic_conversion<mpl::decay_t, mpl::decay_t> {};

template <typename A, typename B> struct safely_constructible<A, B, mpl::enable_if_t<decay_to_ptr::value && decay_to_ptr::value>> : safe_pointer_conversion<mpl::decay_t, mpl::decay_t> {};

template <typename A, typename B> struct safely_constructible<A, B, mpl::enable_if_t<(decay_to_arithmetic::value && decay_to_ptr::value) || (decay_to_ptr::value && decay_to_arithmetic::value)>> : std::false_type {};