From 80085c5168b11b036d2e1fdb1de2687371666a7c Mon Sep 17 00:00:00 2001 From: Josiah Bills Date: Sat, 6 Jun 2020 01:01:26 -0400 Subject: [PATCH 1/3] Used __cpp_lib_is_invocable to ifdef off all places where absl::result_of_t was used in the actual library. --- absl/base/options.h | 2 +- absl/container/internal/btree.h | 7 ++++ absl/meta/type_traits.h | 33 ++++++++++++++---- absl/types/compare.h | 17 ++++++++- absl/types/internal/variant.h | 62 ++++++++++++++++++++------------- 5 files changed, 88 insertions(+), 33 deletions(-) diff --git a/absl/base/options.h b/absl/base/options.h index 230bf1eecc4..10a3d9b7189 100644 --- a/absl/base/options.h +++ b/absl/base/options.h @@ -180,7 +180,7 @@ // absl::variant is a typedef of std::variant, use the feature macro // ABSL_USES_STD_VARIANT. -#define ABSL_OPTION_USE_STD_VARIANT 2 +#define ABSL_OPTION_USE_STD_VARIANT 0 // ABSL_OPTION_USE_INLINE_NAMESPACE diff --git a/absl/container/internal/btree.h b/absl/container/internal/btree.h index f52fe235a2a..06262d04c55 100644 --- a/absl/container/internal/btree.h +++ b/absl/container/internal/btree.h @@ -76,10 +76,17 @@ namespace container_internal { // A helper class that indicates if the Compare parameter is a key-compare-to // comparator. +#if !defined(__cpp_lib_is_invocable) template using btree_is_key_compare_to = std::is_convertible, absl::weak_ordering>; +#else +template +using btree_is_key_compare_to = + std::is_convertible, + absl::weak_ordering>; +#endif struct StringBtreeDefaultLess { using is_transparent = void; diff --git a/absl/meta/type_traits.h b/absl/meta/type_traits.h index ba87d2f0edf..eaa4cc457de 100644 --- a/absl/meta/type_traits.h +++ b/absl/meta/type_traits.h @@ -36,6 +36,7 @@ #define ABSL_META_TYPE_TRAITS_H_ #include + #include #include @@ -244,8 +245,8 @@ template struct disjunction; template -struct disjunction : - std::conditional>::type {}; +struct disjunction + : std::conditional>::type {}; template struct disjunction : T {}; @@ -297,7 +298,7 @@ struct is_function template struct is_trivially_destructible : std::integral_constant::value> { + std::is_destructible::value> { #ifdef ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE private: static constexpr bool compliant = std::is_trivially_destructible::value == @@ -345,9 +346,10 @@ struct is_trivially_destructible // Nontrivially destructible types will cause the expression to be nontrivial. template struct is_trivially_default_constructible - : std::integral_constant::value && - is_trivially_destructible::value> { + : std::integral_constant::value && + is_trivially_destructible::value> { #if defined(ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE) && \ !defined( \ ABSL_META_INTERNAL_STD_CONSTRUCTION_TRAITS_DONT_CHECK_DESTRUCTION) @@ -616,8 +618,25 @@ using common_type_t = typename std::common_type::type; template using underlying_type_t = typename std::underlying_type::type; +#if !defined(__cpp_lib_is_invocable) template using result_of_t = typename std::result_of::type; +#else +template +using invoke_result_t = typename std::invoke_result::type; + +// template +// struct result_of; +// +// template +// struct result_of { +// using invoke_result_t; +//}; +// +// template +// using result_of_t = typename result_of::type; + +#endif namespace type_traits_internal { // In MSVC we can't probe std::hash or stdext::hash because it triggers a @@ -749,8 +768,8 @@ namespace type_traits_internal { // Make the swap-related traits/function accessible from this namespace. using swap_internal::IsNothrowSwappable; using swap_internal::IsSwappable; -using swap_internal::Swap; using swap_internal::StdSwapIsUnconstrained; +using swap_internal::Swap; } // namespace type_traits_internal ABSL_NAMESPACE_END diff --git a/absl/types/compare.h b/absl/types/compare.h index 62ca70f9a70..e80aa6e2883 100644 --- a/absl/types/compare.h +++ b/absl/types/compare.h @@ -569,21 +569,36 @@ constexpr absl::weak_ordering compare_result_as_ordering( const absl::weak_ordering c) { return c; } - +#if !defined(__cpp_lib_is_invocable) template < typename Compare, typename K, typename LK, absl::enable_if_t>::value, int> = 0> +#else +template < + typename Compare, typename K, typename LK, + absl::enable_if_t>::value, + int> = 0> +#endif constexpr absl::weak_ordering do_three_way_comparison(const Compare &compare, const K &x, const LK &y) { return compare_result_as_ordering(compare(x, y)); } +#if !defined(__cpp_lib_is_invocable) template < typename Compare, typename K, typename LK, absl::enable_if_t>::value, int> = 0> +#else +template < + typename Compare, typename K, typename LK, + absl::enable_if_t>::value, + int> = 0> +#endif constexpr absl::weak_ordering do_three_way_comparison(const Compare &compare, const K &x, const LK &y) { return compare(x, y) ? absl::weak_ordering::less diff --git a/absl/types/internal/variant.h b/absl/types/internal/variant.h index 71bd3adfc6e..7ab33efbe69 100644 --- a/absl/types/internal/variant.h +++ b/absl/types/internal/variant.h @@ -198,7 +198,11 @@ using AlwaysZero = SizeT<0>; template struct VisitIndicesResultImpl { +#if !defined(__cpp_lib_is_invocable) using type = absl::result_of_t...)>; +#else + using type = absl::invoke_result_t...>; +#endif }; template @@ -449,7 +453,7 @@ struct FlattenIndices; template struct FlattenIndices { - template + template static constexpr std::size_t Run(std::size_t head, SizeType... tail) { return head + HeadSize * FlattenIndices::Run(tail...); } @@ -498,8 +502,8 @@ struct VisitIndicesVariadicImpl, EndIndices...> { }; template - static VisitIndicesResultT Run( - Op&& op, SizeType... i) { + static VisitIndicesResultT Run(Op&& op, + SizeType... i) { return VisitIndicesSwitch::value>::Run( FlattenedOp{absl::forward(op)}, FlattenIndices<(EndIndices + std::size_t{1})...>::Run( @@ -683,13 +687,13 @@ struct VariantCoreAccess { variant_internal::IndexOfConstructedType; void operator()(SizeT /*old_i*/ - ) const { + ) const { Access(*left) = absl::forward(other); } template void operator()(SizeT /*old_i*/ - ) const { + ) const { using New = typename absl::variant_alternative::type; if (std::is_nothrow_constructible::value || @@ -892,10 +896,15 @@ struct ContainsVariantNPos absl::integer_sequence, absl::integer_sequence>> {}; +#if !defined(__cpp_lib_is_invocable) template using RawVisitResult = absl::result_of_t...)>; - +#else +template +using RawVisitResult = + absl::invoke_result_t...>; +#endif // NOTE: The spec requires that all return-paths yield the same type and is not // SFINAE-friendly, so we can deduce the return type by examining the first // result. If it's not callable, then we get an error, but are compliant and @@ -904,8 +913,13 @@ using RawVisitResult = // at the cost of longer compile-times. template struct VisitResultImpl { +#if !defined(__cpp_lib_is_invocable) using type = absl::result_of_t...)>; +#else + using type = + absl::invoke_result_t...>; +#endif }; // Done in two steps intentionally so that we don't cause substitution to fail. @@ -1159,8 +1173,8 @@ struct VariantHelper> { // CanAccept can't be just an alias because there is a MSVC bug on parameter // pack expansion involving decltype. template - struct CanAccept : - std::integral_constant>::value> {}; + struct CanAccept + : std::integral_constant>::value> {}; // Type metafunction which returns true if Other is an instantiation of // variant, and variants's converting constructor from Other will be @@ -1183,8 +1197,8 @@ struct TrivialMoveOnly { // A union's defaulted copy/move constructor is deleted if any variant member's // copy/move constructor is nontrivial. template -struct IsTriviallyMoveConstructible: - std::is_move_constructible> {}; +struct IsTriviallyMoveConstructible + : std::is_move_constructible> {}; // To guarantee triviality of all special-member functions that can be trivial, // we use a chain of conditional bases for each one. @@ -1419,14 +1433,14 @@ class VariantMoveAssignBaseNontrivial : protected VariantCopyBase { VariantMoveAssignBaseNontrivial& operator=( VariantMoveAssignBaseNontrivial const&) = default; - VariantMoveAssignBaseNontrivial& - operator=(VariantMoveAssignBaseNontrivial&& other) noexcept( - absl::conjunction..., - std::is_nothrow_move_assignable...>::value) { - VisitIndices::Run( - VariantCoreAccess::MakeMoveAssignVisitor(this, &other), other.index_); - return *this; - } + VariantMoveAssignBaseNontrivial& + operator=(VariantMoveAssignBaseNontrivial&& other) noexcept( + absl::conjunction..., + std::is_nothrow_move_assignable...>::value) { + VisitIndices::Run( + VariantCoreAccess::MakeMoveAssignVisitor(this, &other), other.index_); + return *this; + } protected: using Base::index_; @@ -1450,12 +1464,12 @@ class VariantCopyAssignBaseNontrivial : protected VariantMoveAssignBase { VariantCopyAssignBaseNontrivial& operator=( VariantCopyAssignBaseNontrivial&&) = default; - VariantCopyAssignBaseNontrivial& operator=( - const VariantCopyAssignBaseNontrivial& other) { - VisitIndices::Run( - VariantCoreAccess::MakeCopyAssignVisitor(this, other), other.index_); - return *this; - } + VariantCopyAssignBaseNontrivial& operator=( + const VariantCopyAssignBaseNontrivial& other) { + VisitIndices::Run( + VariantCoreAccess::MakeCopyAssignVisitor(this, other), other.index_); + return *this; + } protected: using Base::index_; From 905f06e5f5ac364db7c8bfdee5c9f11dd3a01e59 Mon Sep 17 00:00:00 2001 From: Josiah Bills Date: Sat, 6 Jun 2020 01:43:48 -0400 Subject: [PATCH 2/3] Fixed a couple of result_ofs I had missed in my first pass. Fixed the tests as well. --- absl/base/options.h | 2 +- absl/container/btree_test.cc | 18 +++++++++++++-- absl/container/internal/btree.h | 12 ++++++---- absl/meta/type_traits_test.cc | 39 +++++++++++++++++++++++++-------- absl/types/internal/variant.h | 9 ++++++++ 5 files changed, 64 insertions(+), 16 deletions(-) diff --git a/absl/base/options.h b/absl/base/options.h index 10a3d9b7189..230bf1eecc4 100644 --- a/absl/base/options.h +++ b/absl/base/options.h @@ -180,7 +180,7 @@ // absl::variant is a typedef of std::variant, use the feature macro // ABSL_USES_STD_VARIANT. -#define ABSL_OPTION_USE_STD_VARIANT 0 +#define ABSL_OPTION_USE_STD_VARIANT 2 // ABSL_OPTION_USE_INLINE_NAMESPACE diff --git a/absl/container/btree_test.cc b/absl/container/btree_test.cc index bbdb5f42a62..0db659b696f 100644 --- a/absl/container/btree_test.cc +++ b/absl/container/btree_test.cc @@ -22,8 +22,6 @@ #include #include -#include "gmock/gmock.h" -#include "gtest/gtest.h" #include "absl/base/internal/raw_logging.h" #include "absl/base/macros.h" #include "absl/container/btree_map.h" @@ -38,6 +36,8 @@ #include "absl/strings/str_split.h" #include "absl/strings/string_view.h" #include "absl/types/compare.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" ABSL_FLAG(int, test_values, 10000, "The number of values to use for tests"); @@ -1249,10 +1249,17 @@ void AssertKeyCompareToAdapted() { using Adapted = typename key_compare_to_adapter::type; static_assert(!std::is_same::value, "key_compare_to_adapter should have adapted this comparator."); +#if !defined(__cpp_lib_is_invocable) static_assert( std::is_same>::value, "Adapted comparator should be a key-compare-to comparator."); +#else + static_assert( + std::is_same>::value, + "Adapted comparator should be a key-compare-to comparator."); +#endif } template void AssertKeyCompareToNotAdapted() { @@ -1260,10 +1267,17 @@ void AssertKeyCompareToNotAdapted() { static_assert( std::is_same::value, "key_compare_to_adapter shouldn't have adapted this comparator."); +#if !defined(__cpp_lib_is_invocable) static_assert( std::is_same>::value, "Un-adapted comparator should return bool."); +#else + static_assert( + std::is_same< + bool, absl::invoke_result_t>::value, + "Un-adapted comparator should return bool."); +#endif } TEST(Btree, KeyCompareToAdapter) { diff --git a/absl/container/internal/btree.h b/absl/container/internal/btree.h index 06262d04c55..44e607e8c1b 100644 --- a/absl/container/internal/btree.h +++ b/absl/container/internal/btree.h @@ -432,10 +432,9 @@ class btree_node { // - Otherwise, choose binary. // TODO(ezb): Might make sense to add condition(s) based on node-size. using use_linear_search = std::integral_constant< - bool, - std::is_arithmetic::value && - (std::is_same, key_compare>::value || - std::is_same, key_compare>::value)>; + bool, std::is_arithmetic::value && + (std::is_same, key_compare>::value || + std::is_same, key_compare>::value)>; // This class is organized by gtl::Layout as if it had the following // structure: @@ -1836,8 +1835,13 @@ constexpr bool btree

::static_assert_validation() { "target node size too large"); // Verify that key_compare returns an absl::{weak,strong}_ordering or bool. +#if !defined(__cpp_lib_is_invocable) using compare_result_type = absl::result_of_t; +#else + using compare_result_type = + absl::invoke_result_t; +#endif static_assert( std::is_same::value || std::is_convertible::value, diff --git a/absl/meta/type_traits_test.cc b/absl/meta/type_traits_test.cc index 1aafd0d49a8..4fe99ef3be4 100644 --- a/absl/meta/type_traits_test.cc +++ b/absl/meta/type_traits_test.cc @@ -380,8 +380,8 @@ TEST(TypeTraitsTest, TestIsFunction) { EXPECT_TRUE(absl::is_function::value); EXPECT_TRUE(absl::is_function::value); - EXPECT_FALSE(absl::is_function::value); - EXPECT_FALSE(absl::is_function::value); + EXPECT_FALSE(absl::is_function::value); + EXPECT_FALSE(absl::is_function::value); EXPECT_FALSE(absl::is_function::value); EXPECT_FALSE(absl::is_function::value); } @@ -431,10 +431,10 @@ TEST(TypeTraitsTest, TestTrivialDestructor) { // Verify that simple_pairs of types without trivial destructors // are not marked as trivial. - EXPECT_FALSE((absl::is_trivially_destructible< - simple_pair>::value)); - EXPECT_FALSE((absl::is_trivially_destructible< - simple_pair>::value)); + EXPECT_FALSE( + (absl::is_trivially_destructible>::value)); + EXPECT_FALSE( + (absl::is_trivially_destructible>::value)); // array of such types is trivial using int10 = int[10]; @@ -1126,7 +1126,7 @@ 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)); // NOLINT + ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, int(float)); // NOLINT ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, int(char, ...)); // NOLINT } @@ -1192,6 +1192,8 @@ TEST(TypeTraitsTest, TestUnderlyingType) { ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(underlying_type, enum_long_long); } +#if !defined(__cpp_lib_is_invocable) + struct GetTypeExtT { template absl::result_of_t operator()(T&& arg) const { @@ -1208,6 +1210,26 @@ TEST(TypeTraitsTest, TestResultOf) { EXPECT_EQ(TypeEnum::D, GetTypeExt(Wrap())); } +#else + +struct GetTypeExtT { + template + absl::invoke_result_t operator()(T&& arg) const { + return GetType(std::forward(arg)); + } + + TypeEnum operator()(Wrap) const { return TypeEnum::D; } +} constexpr GetTypeExt = {}; + +TEST(TypeTraitsTest, TestInvokeResult) { + EXPECT_EQ(TypeEnum::A, GetTypeExt(Wrap())); + EXPECT_EQ(TypeEnum::B, GetTypeExt(Wrap())); + EXPECT_EQ(TypeEnum::C, GetTypeExt(Wrap())); + EXPECT_EQ(TypeEnum::D, GetTypeExt(Wrap())); +} + +#endif + template bool TestCopyAssign() { return absl::is_copy_assignable::value == @@ -1286,8 +1308,7 @@ TEST(TypeTraitsTest, IsMoveAssignable) { namespace adl_namespace { -struct DeletedSwap { -}; +struct DeletedSwap {}; void swap(DeletedSwap&, DeletedSwap&) = delete; diff --git a/absl/types/internal/variant.h b/absl/types/internal/variant.h index 7ab33efbe69..ef7d899e8d7 100644 --- a/absl/types/internal/variant.h +++ b/absl/types/internal/variant.h @@ -939,11 +939,20 @@ struct PerformVisitation { template constexpr ReturnType Run(std::false_type /*has_valueless*/, index_sequence, SizeT...) const { +#if !defined(__cpp_lib_is_invocable) static_assert( std::is_same...)>>::value, "All visitation overloads must have the same return type."); +#else + static_assert( + std::is_same< + ReturnType, + absl::invoke_result_t< + Op, VariantAccessResult...>>::value, + "All visitation overloads must have the same return type."); +#endif return absl::base_internal::Invoke( absl::forward(op), VariantCoreAccess::Access( From f3cf8f7bb2bd609798500c592b27d43301952fa8 Mon Sep 17 00:00:00 2001 From: Josiah Bills Date: Sat, 6 Jun 2020 01:49:58 -0400 Subject: [PATCH 3/3] Removed commented out forward-port of result_of_t. --- absl/meta/type_traits.h | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/absl/meta/type_traits.h b/absl/meta/type_traits.h index eaa4cc457de..0341b8dbac2 100644 --- a/absl/meta/type_traits.h +++ b/absl/meta/type_traits.h @@ -624,18 +624,6 @@ using result_of_t = typename std::result_of::type; #else template using invoke_result_t = typename std::invoke_result::type; - -// template -// struct result_of; -// -// template -// struct result_of { -// using invoke_result_t; -//}; -// -// template -// using result_of_t = typename result_of::type; - #endif namespace type_traits_internal {