From 7ec1449d644773aacec801e473ca84438d1d0dec Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Thu, 26 Oct 2017 19:35:23 -0700 Subject: [PATCH] type_t, a generalization of void_t Summary: [Folly] `type_t`, a generalization of `void_t`. Reviewed By: ericniebler Differential Revision: D6082913 fbshipit-source-id: f9557b5da1f6684b12d570b6c1bd52c102cb0703 --- folly/Traits.h | 27 +++++++++++++++++++++++---- folly/test/TraitsTest.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/folly/Traits.h b/folly/Traits.h index d1ab157a..ebf69acf 100644 --- a/folly/Traits.h +++ b/folly/Traits.h @@ -156,10 +156,26 @@ template using _t = typename T::type; /** + * type_t + * + * A type alias for the first template type argument. `type_t` is useful for + * controlling class-template and function-template partial specialization. + * + * Example: + * + * template + * class Container { + * public: + * template + * Container( + * type_t()...))>, + * Args&&...); + * }; + * * void_t * * A type alias for `void`. `void_t` is useful for controling class-template - * partial specialization. + * and function-template partial specialization. * * Example: * @@ -205,13 +221,16 @@ using _t = typename T::type; namespace traits_detail { template -struct void_t_ { - using type = void; +struct type_t_ { + template + using apply = T; }; } // namespace traits_detail +template +using type_t = typename traits_detail::type_t_::template apply; template -using void_t = _t>; +using void_t = type_t; /** * IsRelocatable::value describes the ability of moving around diff --git a/folly/test/TraitsTest.cpp b/folly/test/TraitsTest.cpp index b0214d46..bfb3f7a7 100644 --- a/folly/test/TraitsTest.cpp +++ b/folly/test/TraitsTest.cpp @@ -229,6 +229,16 @@ struct has_value_type : std::false_type {}; template struct has_value_type> : std::true_type {}; + +struct some_tag {}; + +template +struct container { + template + container( + folly::type_t()...))>, + Args&&...) {} +}; } // namespace TEST(Traits, void_t) { @@ -240,3 +250,18 @@ TEST(Traits, void_t) { EXPECT_TRUE((::has_value_type::value)); EXPECT_FALSE((::has_value_type::value)); } + +TEST(Traits, type_t) { + EXPECT_TRUE((::std::is_same, float>::value)); + EXPECT_TRUE((::std::is_same, float>::value)); + EXPECT_TRUE((::std::is_same, float>::value)); + EXPECT_TRUE( + (::std::is_same, float>:: + value)); + EXPECT_TRUE(( + ::std::is_constructible<::container, some_tag, std::string>:: + value)); + EXPECT_FALSE( + (::std::is_constructible<::container, some_tag, float>:: + value)); +} -- 2.34.1