From: Marcelo Juchem <marcelo@fb.com> Date: Wed, 3 Apr 2013 03:22:58 +0000 (-0700) Subject: Implementing a traits class to check for incomplete types X-Git-Tag: v0.22.0~1014 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=622fe822372560c525bd8029d6e68d06d81d24d6;p=folly.git 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 --- 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<T, T1, Ts...> { enum { value = std::is_same<T, T1>::value || IsOneOf<T, Ts...>::value }; }; +/** + * A traits class to check for incomplete types. + * + * Example: + * + * struct FullyDeclared {}; // complete type + * struct ForwardDeclared; // incomplete type + * + * is_complete<int>::value // evaluates to true + * is_complete<FullyDeclared>::value // evaluates to true + * is_complete<ForwardDeclared>::value // evaluates to false + * + * struct ForwardDeclared {}; // declared, at last + * + * is_complete<ForwardDeclared>::value // now it evaluates to true + * + * @author: Marcelo Juchem <marcelo@fb.com> + */ +template <typename T> +class is_complete { + template <unsigned long long> struct sfinae {}; + template <typename U> + constexpr static bool test(sfinae<sizeof(U)>*) { return true; } + template <typename> constexpr static bool test(...) { return false; } +public: + constexpr static bool value = test<T>(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<int>::value)); + EXPECT_TRUE((folly::is_complete<CompleteType>::value)); + EXPECT_FALSE((folly::is_complete<IncompleteType>::value)); +} + int main(int argc, char ** argv) { testing::InitGoogleTest(&argc, argv); google::ParseCommandLineFlags(&argc, &argv, true);