Commit 2d4d958f authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

is_detected

Summary: [Folly] `is_detected`, for a common pattern around `void_t`, backported from Library Fundamentals TS v2.

Reviewed By: Mizuchi

Differential Revision: D23367990

fbshipit-source-id: 3a0e9a5b7eb668f872ce699757e32bb620bb5404
parent 354e50d4
......@@ -356,6 +356,28 @@ using type_t = typename traits_detail::type_t_<T, Ts...>::type;
template <class... Ts>
using void_t = type_t<void, Ts...>;
namespace detail {
template <typename Void, template <typename...> class, typename...>
FOLLY_INLINE_VARIABLE constexpr bool //
is_detected_ = false;
template <template <typename...> class T, typename... A>
FOLLY_INLINE_VARIABLE constexpr bool //
is_detected_<void_t<T<A...>>, T, A...> = true;
} // namespace detail
// is_detected_v
// is_detected
//
// A trait variable and type to test whether some metafunction from types to
// types would succeed or fail in substitution over a given set of arguments.
//
// mimic: std::is_detected, std::is_detected_v, Library Fundamentals TS v2
template <template <typename...> class T, typename... A>
FOLLY_INLINE_VARIABLE constexpr bool is_detected_v =
detail::is_detected_<void, T, A...>;
template <template <typename...> class T, typename... A>
struct is_detected : bool_constant<is_detected_v<T, A...>> {};
template <typename T>
using aligned_storage_for_t =
typename std::aligned_storage<sizeof(T), alignof(T)>::type;
......
......@@ -337,6 +337,21 @@ TEST(Traits, type_t) {
value));
}
namespace {
template <typename T, typename V>
using detector_find = decltype(std::declval<T>().find(std::declval<V>()));
}
TEST(Traits, is_detected) {
EXPECT_TRUE((folly::is_detected<detector_find, std::string, char>::value));
EXPECT_FALSE((folly::is_detected<detector_find, double, char>::value));
}
TEST(Traits, is_detected_v) {
EXPECT_TRUE((folly::is_detected_v<detector_find, std::string, char>));
EXPECT_FALSE((folly::is_detected_v<detector_find, double, char>));
}
TEST(Traits, aligned_storage_for_t) {
struct alignas(2) Foo {
char data[4];
......
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