From 622fe822372560c525bd8029d6e68d06d81d24d6 Mon Sep 17 00:00:00 2001 From: Marcelo Juchem Date: Tue, 2 Apr 2013 20:22:58 -0700 Subject: [PATCH] Implementing a traits class to check for incomplete types Summary: A traits class to check for incomplete types Test Plan: unit tests added Reviewed By: delong.j@fb.com FB internal diff: D760676 --- folly/Traits.h | 28 ++++++++++++++++++++++++++++ folly/test/TraitsTest.cpp | 8 ++++++++ 2 files changed, 36 insertions(+) diff --git a/folly/Traits.h b/folly/Traits.h index bc0a0bf6..ebdb84d1 100644 --- a/folly/Traits.h +++ b/folly/Traits.h @@ -277,6 +277,34 @@ struct IsOneOf { enum { value = std::is_same::value || IsOneOf::value }; }; +/** + * A traits class to check for incomplete types. + * + * Example: + * + * struct FullyDeclared {}; // complete type + * struct ForwardDeclared; // incomplete type + * + * is_complete::value // evaluates to true + * is_complete::value // evaluates to true + * is_complete::value // evaluates to false + * + * struct ForwardDeclared {}; // declared, at last + * + * is_complete::value // now it evaluates to true + * + * @author: Marcelo Juchem + */ +template +class is_complete { + template struct sfinae {}; + template + constexpr static bool test(sfinae*) { return true; } + template constexpr static bool test(...) { return false; } +public: + constexpr static bool value = test(nullptr); +}; + /* * Complementary type traits to check for a negative/non-positive value. * diff --git a/folly/test/TraitsTest.cpp b/folly/test/TraitsTest.cpp index a2760ca8..7a80c6cc 100644 --- a/folly/test/TraitsTest.cpp +++ b/folly/test/TraitsTest.cpp @@ -96,6 +96,14 @@ TEST(Traits, is_negative) { EXPECT_FALSE(folly::is_non_positive(1u)); } +struct CompleteType {}; +struct IncompleteType; +TEST(Traits, is_complete) { + EXPECT_TRUE((folly::is_complete::value)); + EXPECT_TRUE((folly::is_complete::value)); + EXPECT_FALSE((folly::is_complete::value)); +} + int main(int argc, char ** argv) { testing::InitGoogleTest(&argc, argv); google::ParseCommandLineFlags(&argc, &argv, true); -- 2.34.1