Commit 67a1d1d5 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

is_constexpr_default_constructible

Summary: [Folly] `is_constexpr_default_constructible`, for deciding whether a type is constexpr default-constructible.

Reviewed By: vitaut

Differential Revision: D18092652

fbshipit-source-id: a4642726a9495135abca1506c5dbb3d57c0e0558
parent db30bf97
......@@ -168,6 +168,45 @@ constexpr bool is_instantiation_of_v = is_instantiation_of<C, T>::value;
} // namespace detail
namespace detail {
template <bool, typename T>
struct is_constexpr_default_constructible_;
template <typename T>
struct is_constexpr_default_constructible_<false, T> {
using type = std::false_type;
};
template <typename T>
struct is_constexpr_default_constructible_<true, T> {
static constexpr int take(T) {
return 0;
}
template <int = take(T{})>
static std::true_type sfinae(int);
static std::false_type sfinae(...);
using type = decltype(sfinae(0));
};
} // namespace detail
// is_constexpr_default_constructible
// is_constexpr_default_constructible_v
//
// A type trait, with associated variable template, which determines whether
// its type parameter is constexpr default-constructible, that is, default-
// constructible in a constexpr context.
//
// Instantiations of is_constexpr_default_constructible unambiguously inherit
// std::integral_constant<bool, V> for some bool V.
template <typename T>
struct is_constexpr_default_constructible
: detail::is_constexpr_default_constructible_<
std::is_default_constructible<T>::value,
T>::type {};
template <typename T>
FOLLY_INLINE_VARIABLE constexpr bool is_constexpr_default_constructible_v =
is_constexpr_default_constructible<T>::value;
/***
* _t
*
......
......@@ -401,3 +401,32 @@ TEST(Traits, is_instantiation_of) {
EXPECT_TRUE((detail::is_instantiation_of_v<A, A<int>>));
EXPECT_FALSE((detail::is_instantiation_of_v<A, B>));
}
TEST(Traits, is_constexpr_default_constructible) {
EXPECT_TRUE(is_constexpr_default_constructible_v<int>);
struct Empty {};
EXPECT_TRUE(is_constexpr_default_constructible_v<Empty>);
struct NonTrivialDtor {
~NonTrivialDtor() {}
};
EXPECT_FALSE(is_constexpr_default_constructible_v<NonTrivialDtor>);
struct ConstexprCtor {
int x, y;
constexpr ConstexprCtor() noexcept : x(7), y(11) {}
};
EXPECT_TRUE(is_constexpr_default_constructible_v<ConstexprCtor>);
struct NonConstexprCtor {
int x, y;
NonConstexprCtor() noexcept : x(7), y(11) {}
};
EXPECT_FALSE(is_constexpr_default_constructible_v<NonConstexprCtor>);
struct NoDefaultCtor {
constexpr NoDefaultCtor(int, int) noexcept {}
};
EXPECT_FALSE(is_constexpr_default_constructible_v<NoDefaultCtor>);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment