From 5839c1b498fff818924032d340cb1ee7027a6c8b Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Fri, 10 Jul 2015 21:24:17 -0700 Subject: [PATCH 1/1] An has_member_type macro. Summary: [Folly] An has_member_type macro. Reviewed By: @juchem Differential Revision: D2229775 --- folly/Traits.h | 10 ++++++++++ folly/test/TraitsTest.cpp | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/folly/Traits.h b/folly/Traits.h index 309157c4..fd2be06b 100644 --- a/folly/Traits.h +++ b/folly/Traits.h @@ -432,6 +432,16 @@ FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::function); // Boost FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(boost::shared_ptr); +#define FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(classname, type_name) \ + template \ + struct classname { \ + template \ + constexpr static bool test(typename C::type_name*) { return true; } \ + template \ + constexpr static bool test(...) { return false; } \ + constexpr static bool value = test(nullptr); \ + } + #define FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, cv_qual) \ template \ class classname { \ diff --git a/folly/test/TraitsTest.cpp b/folly/test/TraitsTest.cpp index 83ffb43d..e3776e2b 100644 --- a/folly/test/TraitsTest.cpp +++ b/folly/test/TraitsTest.cpp @@ -110,6 +110,15 @@ TEST(Traits, relational) { EXPECT_FALSE((folly::greater_than(254u))); } +struct membership_no {}; +struct membership_yes { using x = void; }; +FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(has_member_type_x, x); + +TEST(Traits, has_member_type) { + EXPECT_FALSE(bool(has_member_type_x::value)); + EXPECT_TRUE(bool(has_member_type_x::value)); +} + int main(int argc, char ** argv) { testing::InitGoogleTest(&argc, argv); gflags::ParseCommandLineFlags(&argc, &argv, true); -- 2.34.1