diff options
Diffstat (limited to 'absl/meta')
-rw-r--r-- | absl/meta/type_traits.h | 74 | ||||
-rw-r--r-- | absl/meta/type_traits_test.cc | 161 |
2 files changed, 228 insertions, 7 deletions
diff --git a/absl/meta/type_traits.h b/absl/meta/type_traits.h index 923234c4..231e08db 100644 --- a/absl/meta/type_traits.h +++ b/absl/meta/type_traits.h @@ -42,9 +42,10 @@ #include "absl/base/config.h" namespace absl { -inline namespace lts_2018_06_20 { +inline namespace lts_2018_12_18 { namespace type_traits_internal { + template <typename... Ts> struct VoidTImpl { using type = void; @@ -62,8 +63,68 @@ struct default_alignment_of_aligned_storage<Len, static constexpr size_t value = Align; }; +//////////////////////////////// +// Library Fundamentals V2 TS // +//////////////////////////////// + +// NOTE: The `is_detected` family of templates here differ from the library +// fundamentals specification in that for library fundamentals, `Op<Args...>` is +// evaluated as soon as the type `is_detected<Op, Args...>` undergoes +// substitution, regardless of whether or not the `::value` is accessed. That +// is inconsistent with all other standard traits and prevents lazy evaluation +// in larger contexts (such as if the `is_detected` check is a trailing argument +// of a `conjunction`. This implementation opts to instead be lazy in the same +// way that the standard traits are (this "defect" of the detection idiom +// specifications has been reported). + +template <class Enabler, template <class...> class Op, class... Args> +struct is_detected_impl { + using type = std::false_type; +}; + +template <template <class...> class Op, class... Args> +struct is_detected_impl<typename VoidTImpl<Op<Args...>>::type, Op, Args...> { + using type = std::true_type; +}; + +template <template <class...> class Op, class... Args> +struct is_detected : is_detected_impl<void, Op, Args...>::type {}; + +template <class Enabler, class To, template <class...> class Op, class... Args> +struct is_detected_convertible_impl { + using type = std::false_type; +}; + +template <class To, template <class...> class Op, class... Args> +struct is_detected_convertible_impl< + typename std::enable_if<std::is_convertible<Op<Args...>, To>::value>::type, + To, Op, Args...> { + using type = std::true_type; +}; + +template <class To, template <class...> class Op, class... Args> +struct is_detected_convertible + : is_detected_convertible_impl<void, To, Op, Args...>::type {}; + +template <typename T> +using IsCopyAssignableImpl = + decltype(std::declval<T&>() = std::declval<const T&>()); + +template <typename T> +using IsMoveAssignableImpl = decltype(std::declval<T&>() = std::declval<T&&>()); + } // namespace type_traits_internal +template <typename T> +struct is_copy_assignable : type_traits_internal::is_detected< + type_traits_internal::IsCopyAssignableImpl, T> { +}; + +template <typename T> +struct is_move_assignable : type_traits_internal::is_detected< + type_traits_internal::IsMoveAssignableImpl, T> { +}; + // void_t() // // Ignores the type of any its arguments and returns `void`. In general, this @@ -264,8 +325,9 @@ struct is_trivially_copy_constructible // `is_trivially_assignable<T&, const T&>`. template <typename T> struct is_trivially_copy_assignable - : std::integral_constant<bool, __has_trivial_assign(T) && - std::is_copy_assignable<T>::value> { + : std::integral_constant< + bool, __has_trivial_assign(typename std::remove_reference<T>::type) && + absl::is_copy_assignable<T>::value> { #ifdef ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE private: static constexpr bool compliant = @@ -365,10 +427,12 @@ struct IsHashEnabled : absl::conjunction<std::is_default_constructible<std::hash<Key>>, std::is_copy_constructible<std::hash<Key>>, std::is_destructible<std::hash<Key>>, - std::is_copy_assignable<std::hash<Key>>, + absl::is_copy_assignable<std::hash<Key>>, IsHashable<Key>> {}; + } // namespace type_traits_internal -} // inline namespace lts_2018_06_20 +} // inline namespace lts_2018_12_18 } // namespace absl + #endif // ABSL_META_TYPE_TRAITS_H_ diff --git a/absl/meta/type_traits_test.cc b/absl/meta/type_traits_test.cc index c44d1c5f..f51f5ded 100644 --- a/absl/meta/type_traits_test.cc +++ b/absl/meta/type_traits_test.cc @@ -34,6 +34,83 @@ struct simple_pair { struct Dummy {}; +struct ReturnType {}; +struct ConvertibleToReturnType { + operator ReturnType() const; // NOLINT +}; + +// Unique types used as parameter types for testing the detection idiom. +struct StructA {}; +struct StructB {}; +struct StructC {}; + +struct TypeWithBarFunction { + template <class T, + absl::enable_if_t<std::is_same<T&&, StructA&>::value, int> = 0> + ReturnType bar(T&&, const StructB&, StructC&&) &&; // NOLINT +}; + +struct TypeWithBarFunctionAndConvertibleReturnType { + template <class T, + absl::enable_if_t<std::is_same<T&&, StructA&>::value, int> = 0> + ConvertibleToReturnType bar(T&&, const StructB&, StructC&&) &&; // NOLINT +}; + +template <class Class, class... Ts> +using BarIsCallableImpl = + decltype(std::declval<Class>().bar(std::declval<Ts>()...)); + +template <class Class, class... T> +using BarIsCallable = + absl::type_traits_internal::is_detected<BarIsCallableImpl, Class, T...>; + +template <class Class, class... T> +using BarIsCallableConv = absl::type_traits_internal::is_detected_convertible< + ReturnType, BarIsCallableImpl, Class, T...>; + +// NOTE: Test of detail type_traits_internal::is_detected. +TEST(IsDetectedTest, BasicUsage) { + EXPECT_TRUE((BarIsCallable<TypeWithBarFunction, StructA&, const StructB&, + StructC>::value)); + EXPECT_TRUE( + (BarIsCallable<TypeWithBarFunction, StructA&, StructB&, StructC>::value)); + EXPECT_TRUE( + (BarIsCallable<TypeWithBarFunction, StructA&, StructB, StructC>::value)); + + EXPECT_FALSE((BarIsCallable<int, StructA&, const StructB&, StructC>::value)); + EXPECT_FALSE((BarIsCallable<TypeWithBarFunction&, StructA&, const StructB&, + StructC>::value)); + EXPECT_FALSE((BarIsCallable<TypeWithBarFunction, StructA, const StructB&, + StructC>::value)); +} + +// NOTE: Test of detail type_traits_internal::is_detected_convertible. +TEST(IsDetectedConvertibleTest, BasicUsage) { + EXPECT_TRUE((BarIsCallableConv<TypeWithBarFunction, StructA&, const StructB&, + StructC>::value)); + EXPECT_TRUE((BarIsCallableConv<TypeWithBarFunction, StructA&, StructB&, + StructC>::value)); + EXPECT_TRUE((BarIsCallableConv<TypeWithBarFunction, StructA&, StructB, + StructC>::value)); + EXPECT_TRUE((BarIsCallableConv<TypeWithBarFunctionAndConvertibleReturnType, + StructA&, const StructB&, StructC>::value)); + EXPECT_TRUE((BarIsCallableConv<TypeWithBarFunctionAndConvertibleReturnType, + StructA&, StructB&, StructC>::value)); + EXPECT_TRUE((BarIsCallableConv<TypeWithBarFunctionAndConvertibleReturnType, + StructA&, StructB, StructC>::value)); + + EXPECT_FALSE( + (BarIsCallableConv<int, StructA&, const StructB&, StructC>::value)); + EXPECT_FALSE((BarIsCallableConv<TypeWithBarFunction&, StructA&, + const StructB&, StructC>::value)); + EXPECT_FALSE((BarIsCallableConv<TypeWithBarFunction, StructA, const StructB&, + StructC>::value)); + EXPECT_FALSE((BarIsCallableConv<TypeWithBarFunctionAndConvertibleReturnType&, + StructA&, const StructB&, StructC>::value)); + EXPECT_FALSE((BarIsCallableConv<TypeWithBarFunctionAndConvertibleReturnType, + StructA, const StructB&, StructC>::value)); +} + TEST(VoidTTest, BasicUsage) { StaticAssertTypeEq<void, absl::void_t<Dummy>>(); StaticAssertTypeEq<void, absl::void_t<Dummy, Dummy, Dummy>>(); @@ -528,6 +605,10 @@ TEST(TypeTraitsTest, TestTrivialCopyAssign) { // Verify that arrays are not trivially copy assignable using int10 = int[10]; EXPECT_FALSE(absl::is_trivially_copy_assignable<int10>::value); + + // Verify that references are handled correctly + EXPECT_TRUE(absl::is_trivially_copy_assignable<Trivial&&>::value); + EXPECT_TRUE(absl::is_trivially_copy_assignable<Trivial&>::value); } #define ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(trait_name, ...) \ @@ -714,8 +795,8 @@ TEST(TypeTraitsTest, TestDecay) { ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, int[][1]); ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, int()); - ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, int(float)); - ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, int(char, ...)); + ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, int(float)); // NOLINT + ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, int(char, ...)); // NOLINT } struct TypeA {}; @@ -796,4 +877,80 @@ TEST(TypeTraitsTest, TestResultOf) { EXPECT_EQ(TypeEnum::D, GetTypeExt(Wrap<TypeD>())); } +template <typename T> +bool TestCopyAssign() { + return absl::is_copy_assignable<T>::value == + std::is_copy_assignable<T>::value; +} + +TEST(TypeTraitsTest, IsCopyAssignable) { + EXPECT_TRUE(TestCopyAssign<int>()); + EXPECT_TRUE(TestCopyAssign<int&>()); + EXPECT_TRUE(TestCopyAssign<int&&>()); + + struct S {}; + EXPECT_TRUE(TestCopyAssign<S>()); + EXPECT_TRUE(TestCopyAssign<S&>()); + EXPECT_TRUE(TestCopyAssign<S&&>()); + + class C { + public: + explicit C(C* c) : c_(c) {} + ~C() { delete c_; } + + private: + C* c_; + }; + EXPECT_TRUE(TestCopyAssign<C>()); + EXPECT_TRUE(TestCopyAssign<C&>()); + EXPECT_TRUE(TestCopyAssign<C&&>()); + + // Reason for ifndef: add_lvalue_reference<T> in libc++ breaks for these cases +#ifndef _LIBCPP_VERSION + EXPECT_TRUE(TestCopyAssign<int()>()); + EXPECT_TRUE(TestCopyAssign<int(int) const>()); + EXPECT_TRUE(TestCopyAssign<int(...) volatile&>()); + EXPECT_TRUE(TestCopyAssign<int(int, ...) const volatile&&>()); +#endif // _LIBCPP_VERSION +} + +template <typename T> +bool TestMoveAssign() { + return absl::is_move_assignable<T>::value == + std::is_move_assignable<T>::value; +} + +TEST(TypeTraitsTest, IsMoveAssignable) { + EXPECT_TRUE(TestMoveAssign<int>()); + EXPECT_TRUE(TestMoveAssign<int&>()); + EXPECT_TRUE(TestMoveAssign<int&&>()); + + struct S {}; + EXPECT_TRUE(TestMoveAssign<S>()); + EXPECT_TRUE(TestMoveAssign<S&>()); + EXPECT_TRUE(TestMoveAssign<S&&>()); + + class C { + public: + explicit C(C* c) : c_(c) {} + ~C() { delete c_; } + void operator=(const C&) = delete; + void operator=(C&&) = delete; + + private: + C* c_; + }; + EXPECT_TRUE(TestMoveAssign<C>()); + EXPECT_TRUE(TestMoveAssign<C&>()); + EXPECT_TRUE(TestMoveAssign<C&&>()); + + // Reason for ifndef: add_lvalue_reference<T> in libc++ breaks for these cases +#ifndef _LIBCPP_VERSION + EXPECT_TRUE(TestMoveAssign<int()>()); + EXPECT_TRUE(TestMoveAssign<int(int) const>()); + EXPECT_TRUE(TestMoveAssign<int(...) volatile&>()); + EXPECT_TRUE(TestMoveAssign<int(int, ...) const volatile&&>()); +#endif // _LIBCPP_VERSION +} + } // namespace |