From 7c7754fb3ed9ffb57d35fe8658f3ba4d73a31e72 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Wed, 13 Mar 2019 14:59:43 -0700 Subject: Export of internal Abseil changes. -- 89b5e681db1d4f0b039daebb86c49bda77c8931b by Tom Manshreck : Add clarification that absl::Hash does not produce stable values across instances. PiperOrigin-RevId: 238316564 -- 56dec1d1e37fb2a02aa10d7c81bcd78f8486c093 by Eric Fiselier : Add SFINAE to absl::optional::optional(in_place_t, Args...) This constructor previously didn't have SFINAE because the SFINAE acted badly when Clang was trying to figure out what special members to declare. Specifically, while considering copy constructors it would attempt to call `optional(in_place_t) [ with Args = <> ]`, which evaluated `is_constructible`, which shouldn't have been evaluated. This patch avoids the eager SFINAE bug by deducing the in_place_t tag and short-circuting the SFINAE if the argument passed is not exactly in_place_t. I fixed the same bug in libc++ in the same way. PiperOrigin-RevId: 238290810 -- ffe6d087df495f7f990c89b0a4e1f1664c2c4f9d by Jon Cohen : Remove absl_internal_blah names form CMake. We are always creating the alias target now, since use of these targets still requires including a header with internal in the name. This simplifies target naming a bit, especially for installation where we have to generate non-prefixed target names to export in the absl:: namespace. PiperOrigin-RevId: 238280135 -- 9d8ae92ff8727fa49391f7f5386810ff81e80aa7 by Derek Mauro : Use ABSL_TEST_COPTS for spinlock_benchmark_common and mutex_benchmark_common. Despite being cc_library, these are really tests and the warning for the used-but-marked-unused iterator in Google Benchmark needs to be supressed. PiperOrigin-RevId: 238225200 -- fcde1a79420ce15c8925944c45b69f9fd5226f12 by Matt Armstrong : Qualify calls to certain functions from the cmath library. PiperOrigin-RevId: 238163972 -- 4b931e5ef4ba76961b0e2a9edab1e586ba12dfd4 by Tom Manshreck : Add clarification that absl::Hash does not produce stable values across instances. PiperOrigin-RevId: 238125817 -- 8963ea8c65cac1e396a72fe77d6eb6a7313d76db by Derek Mauro : Fix -Wc++14-binary-literal warning. PiperOrigin-RevId: 238069157 GitOrigin-RevId: 89b5e681db1d4f0b039daebb86c49bda77c8931b Change-Id: Ib06f1ee8efcddb7e2f332bc5bf1c1325458e1073 --- absl/types/optional.h | 9 ++++---- absl/types/optional_test.cc | 53 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 49 insertions(+), 13 deletions(-) (limited to 'absl/types') diff --git a/absl/types/optional.h b/absl/types/optional.h index a86dea9..c048879 100644 --- a/absl/types/optional.h +++ b/absl/types/optional.h @@ -513,10 +513,11 @@ class optional : private optional_internal::optional_data, // the arguments `std::forward(args)...` within the `optional`. // (The `in_place_t` is a tag used to indicate that the contained object // should be constructed in-place.) - // - // TODO(absl-team): Add std::is_constructible SFINAE. - template - constexpr explicit optional(in_place_t, Args&&... args) + template , + std::is_constructible >::value>* = nullptr> + constexpr explicit optional(InPlaceT, Args&&... args) : data_base(in_place_t(), absl::forward(args)...) {} // Constructs a non-empty `optional` direct-initialized value of type `T` from diff --git a/absl/types/optional_test.cc b/absl/types/optional_test.cc index b93aa98..68842ab 100644 --- a/absl/types/optional_test.cc +++ b/absl/types/optional_test.cc @@ -157,6 +157,16 @@ struct NonMovable { NonMovable& operator=(NonMovable&&) = delete; }; +struct NoDefault { + NoDefault() = delete; + NoDefault(const NoDefault&) {} + NoDefault& operator=(const NoDefault&) { return *this; } +}; + +struct ConvertsFromInPlaceT { + ConvertsFromInPlaceT(absl::in_place_t) {} // NOLINT +}; + TEST(optionalTest, DefaultConstructor) { absl::optional empty; EXPECT_FALSE(empty); @@ -337,16 +347,18 @@ TEST(optionalTest, InPlaceConstructor) { static_assert((*opt2).x == ConstexprType::kCtorInitializerList, ""); #endif - // TODO(absl-team): uncomment these when std::is_constructible - // SFINAE is added to optional::optional(absl::in_place_t, Args&&...). - // struct I { - // I(absl::in_place_t); - // }; + EXPECT_FALSE((std::is_constructible, + absl::in_place_t>::value)); + EXPECT_FALSE((std::is_constructible, + const absl::in_place_t&>::value)); + EXPECT_TRUE( + (std::is_constructible, + absl::in_place_t, absl::in_place_t>::value)); - // EXPECT_FALSE((std::is_constructible, - // absl::in_place_t>::value)); - // EXPECT_FALSE((std::is_constructible, const - // absl::in_place_t&>::value)); + EXPECT_FALSE((std::is_constructible, + absl::in_place_t>::value)); + EXPECT_FALSE((std::is_constructible, + absl::in_place_t&&>::value)); } // template optional(U&&); @@ -1624,4 +1636,27 @@ TEST(optionalTest, AssignmentConstraints) { EXPECT_TRUE(absl::is_copy_assignable>::value); } +struct NestedClassBug { + struct Inner { + bool dummy = false; + }; + absl::optional value; +}; + +TEST(optionalTest, InPlaceTSFINAEBug) { + NestedClassBug b; + ((void)b); + using Inner = NestedClassBug::Inner; + + EXPECT_TRUE((std::is_default_constructible::value)); + EXPECT_TRUE((std::is_constructible::value)); + EXPECT_TRUE( + (std::is_constructible, absl::in_place_t>::value)); + + absl::optional o(absl::in_place); + EXPECT_TRUE(o.has_value()); + o.emplace(); + EXPECT_TRUE(o.has_value()); +} + } // namespace -- cgit v1.2.3