diff --git a/folly/Traits.h b/folly/Traits.h index 309157c..58e9418 100644 --- a/folly/Traits.h +++ b/folly/Traits.h @@ -290,6 +290,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 for integral comparisons. * diff --git a/folly/test/TraitsTest.cpp b/folly/test/TraitsTest.cpp index 83ffb43..695e36d 100644 --- a/folly/test/TraitsTest.cpp +++ b/folly/test/TraitsTest.cpp @@ -110,6 +110,14 @@ TEST(Traits, relational) { EXPECT_FALSE((folly::greater_than(254u))); } +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); gflags::ParseCommandLineFlags(&argc, &argv, true);