chromium/chromium-65.0.3325.146-Implement-converting-constructors-from-Optional-U.patch
2018-03-15 12:17:21 -04:00

117 lines
4.6 KiB
Diff

diff -up chromium-65.0.3325.146/base/optional.h.converting chromium-65.0.3325.146/base/optional.h
--- chromium-65.0.3325.146/base/optional.h.converting 2018-03-13 22:31:05.248797490 -0400
+++ chromium-65.0.3325.146/base/optional.h 2018-03-13 22:33:10.826368771 -0400
@@ -31,6 +31,10 @@ constexpr in_place_t in_place = {};
// http://en.cppreference.com/w/cpp/utility/optional/nullopt
constexpr nullopt_t nullopt(0);
+// Forward declaration, which is refered by following helpers.
+template <typename T>
+class Optional;
+
namespace internal {
template <typename T, bool = std::is_trivially_destructible<T>::value>
@@ -220,6 +224,19 @@ class OptionalBase {
constexpr explicit OptionalBase(in_place_t, Args&&... args)
: storage_(in_place, std::forward<Args>(args)...) {}
+ // Implementation of converting constructors.
+ template <typename U>
+ explicit OptionalBase(const OptionalBase<U>& other) {
+ if (other.storage_.is_populated_)
+ storage_.Init(other.storage_.value_);
+ }
+
+ template <typename U>
+ explicit OptionalBase(OptionalBase<U>&& other) {
+ if (other.storage_.is_populated_)
+ storage_.Init(std::move(other.storage_.value_));
+ }
+
~OptionalBase() = default;
OptionalBase& operator=(const OptionalBase& other) {
@@ -263,6 +280,11 @@ class OptionalBase {
storage_.is_populated_ = false;
}
+ // For implementing conversion, allow access to other typed OptionalBase
+ // class.
+ template <typename U>
+ friend class OptionalBase;
+
OptionalStorage<T> storage_;
};
@@ -318,6 +340,20 @@ struct MoveAssignable<false> {
MoveAssignable& operator=(MoveAssignable&&) = delete;
};
+// Helper to conditionally enable converting constructors.
+template <typename T, typename U>
+struct IsConvertibleFromOptional
+ : std::integral_constant<
+ bool,
+ std::is_constructible<T, Optional<U>&>::value ||
+ std::is_constructible<T, const Optional<U>&>::value ||
+ std::is_constructible<T, Optional<U>&&>::value ||
+ std::is_constructible<T, const Optional<U>&&>::value ||
+ std::is_convertible<Optional<U>&, T>::value ||
+ std::is_convertible<const Optional<U>&, T>::value ||
+ std::is_convertible<Optional<U>&&, T>::value ||
+ std::is_convertible<const Optional<U>&&, T>::value> {};
+
} // namespace internal
// base::Optional is a Chromium version of the C++17 optional class:
@@ -348,7 +384,47 @@ class Optional
constexpr Optional(const Optional& other) = default;
constexpr Optional(Optional&& other) = default;
- constexpr Optional(nullopt_t) {}
+ constexpr Optional(nullopt_t) {} // NOLINT(runtime/explicit)
+
+ // Converting copy constructor. "explicit" only if
+ // std::is_convertible<const U&, T>::value is false. It is implemented by
+ // declaring two almost same constructors, but that condition in enable_if_t
+ // is different, so that either one is chosen, thanks to SFINAE.
+ template <
+ typename U,
+ std::enable_if_t<std::is_constructible<T, const U&>::value &&
+ !internal::IsConvertibleFromOptional<T, U>::value &&
+ std::is_convertible<const U&, T>::value,
+ bool> = false>
+ Optional(const Optional<U>& other) : internal::OptionalBase<T>(other) {}
+
+ template <
+ typename U,
+ std::enable_if_t<std::is_constructible<T, const U&>::value &&
+ !internal::IsConvertibleFromOptional<T, U>::value &&
+ !std::is_convertible<const U&, T>::value,
+ bool> = false>
+ explicit Optional(const Optional<U>& other)
+ : internal::OptionalBase<T>(other) {}
+
+ // Converting move constructor. Similar to converting copy constructor,
+ // declaring two (explicit and non-explicit) constructors.
+ template <
+ typename U,
+ std::enable_if_t<std::is_constructible<T, U&&>::value &&
+ !internal::IsConvertibleFromOptional<T, U>::value &&
+ std::is_convertible<U&&, T>::value,
+ bool> = false>
+ Optional(Optional<U>&& other) : internal::OptionalBase<T>(std::move(other)) {}
+
+ template <
+ typename U,
+ std::enable_if_t<std::is_constructible<T, U&&>::value &&
+ !internal::IsConvertibleFromOptional<T, U>::value &&
+ !std::is_convertible<U&&, T>::value,
+ bool> = false>
+ explicit Optional(Optional<U>&& other)
+ : internal::OptionalBase<T>(std::move(other)) {}
constexpr Optional(const T& value)
: internal::OptionalBase<T>(in_place, value) {}