From eed36bfba04dbe8a08ec72e631c3b337e4f2db11 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Thu, 15 Mar 2018 13:48:55 -0700 Subject: Changes imported from Abseil "staging" branch: - 95e20a66bfa6104937ec92722b04a9c0ca0443dc Make ThrowingBool convertible to bool. by Xiaoyi Zhang GitOrigin-RevId: 95e20a66bfa6104937ec92722b04a9c0ca0443dc Change-Id: I7a1dcb53b0764b24482ee1886276b267c8cb83c2 --- absl/base/exception_safety_testing_test.cc | 13 +++++++++++++ absl/base/internal/exception_safety_testing.h | 8 +++++--- 2 files changed, 18 insertions(+), 3 deletions(-) (limited to 'absl/base') diff --git a/absl/base/exception_safety_testing_test.cc b/absl/base/exception_safety_testing_test.cc index 5477b40a..f8baf20c 100644 --- a/absl/base/exception_safety_testing_test.cc +++ b/absl/base/exception_safety_testing_test.cc @@ -18,6 +18,7 @@ #include #include #include +#include #include #include "gtest/gtest-spi.h" @@ -683,6 +684,18 @@ TEST(AllocInspectorTest, ConstructedTwice) { new (&storage) Tracked; }, "re-constructed"); + reinterpret_cast(&storage)->~Tracked(); } + +TEST(ThrowingValueTraitsTest, RelationalOperators) { + ThrowingValue<> a, b; + EXPECT_TRUE((std::is_convertible::value)); + EXPECT_TRUE((std::is_convertible::value)); + EXPECT_TRUE((std::is_convertible::value)); + EXPECT_TRUE((std::is_convertible::value)); + EXPECT_TRUE((std::is_convertible b), bool>::value)); + EXPECT_TRUE((std::is_convertible= b), bool>::value)); +} + } // namespace } // namespace absl diff --git a/absl/base/internal/exception_safety_testing.h b/absl/base/internal/exception_safety_testing.h index 8eac2264..adee848b 100644 --- a/absl/base/internal/exception_safety_testing.h +++ b/absl/base/internal/exception_safety_testing.h @@ -203,12 +203,12 @@ extern exceptions_internal::NoThrowTag no_throw_ctor; inline void SetCountdown() { exceptions_internal::countdown = 0; } inline void UnsetCountdown() { exceptions_internal::countdown = -1; } -// A test class which is contextually convertible to bool. The conversion can -// be instrumented to throw at a controlled time. +// A test class which is convertible to bool. The conversion can be +// instrumented to throw at a controlled time. class ThrowingBool { public: ThrowingBool(bool b) noexcept : b_(b) {} // NOLINT(runtime/explicit) - explicit operator bool() const { + operator bool() const { // NOLINT(runtime/explicit) exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION); return b_; } @@ -355,6 +355,8 @@ class ThrowingValue : private exceptions_internal::TrackedObject { } // Comparison Operators + // NOTE: We use `ThrowingBool` instead of `bool` because most STL + // types/containers requires T to be convertible to bool. friend ThrowingBool operator==(const ThrowingValue& a, const ThrowingValue& b) { exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION); -- cgit v1.2.3