[libcxx-commits] [libcxx] [libc++] restrict the expected conversion constructor not compete against copy constructor (PR #96101)

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jun 19 11:51:02 PDT 2024


https://github.com/huixie90 created https://github.com/llvm/llvm-project/pull/96101

None

>From 05b308a681cb10774b0dafa307c432aa7d6b211f Mon Sep 17 00:00:00 2001
From: Hui <hui.xie0621 at gmail.com>
Date: Wed, 19 Jun 2024 19:50:23 +0100
Subject: [PATCH] [libc++] restrict the expected conversion constructor not
 compete against copy constructor

---
 libcxx/include/__expected/expected.h           |  4 +++-
 .../expected.expected/ctor/ctor.copy.pass.cpp  | 18 ++++++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/libcxx/include/__expected/expected.h b/libcxx/include/__expected/expected.h
index 0f994e297a877..da0a0fa85714c 100644
--- a/libcxx/include/__expected/expected.h
+++ b/libcxx/include/__expected/expected.h
@@ -507,7 +507,9 @@ class expected : private __expected_base<_Tp, _Err> {
       _And< is_constructible<_Tp, _UfQual>,
             is_constructible<_Err, _OtherErrQual>,
             _If<_Not<is_same<remove_cv_t<_Tp>, bool>>::value,
-                _And< _Not<is_constructible<_Tp, expected<_Up, _OtherErr>&>>,
+                _And< 
+                      _Not<_And<is_same<_Tp, _Up>, is_same<_Err, _OtherErr>>>,
+                      _Not<is_constructible<_Tp, expected<_Up, _OtherErr>&>>,
                       _Not<is_constructible<_Tp, expected<_Up, _OtherErr>>>,
                       _Not<is_constructible<_Tp, const expected<_Up, _OtherErr>&>>,
                       _Not<is_constructible<_Tp, const expected<_Up, _OtherErr>>>,
diff --git a/libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.copy.pass.cpp b/libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.copy.pass.cpp
index 581df51207da2..6c278be9de98a 100644
--- a/libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.copy.pass.cpp
+++ b/libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.copy.pass.cpp
@@ -62,6 +62,17 @@ static_assert(!std::is_trivially_copy_constructible_v<std::expected<CopyableNonT
 static_assert(!std::is_trivially_copy_constructible_v<std::expected<int, CopyableNonTrivial>>);
 static_assert(!std::is_trivially_copy_constructible_v<std::expected<CopyableNonTrivial, CopyableNonTrivial>>);
 
+
+struct Any {
+  constexpr Any() = default;
+  constexpr Any(const Any&) = default;
+  constexpr Any& operator=(const Any&)=default;
+
+  template <class T>
+    requires (!std::is_same_v<Any, std::decay_t<T>> && std::is_copy_constructible_v<std::decay_t<T>>)
+  constexpr Any(T&&){}
+};
+
 constexpr bool test() {
   // copy the value non-trivial
   {
@@ -109,6 +120,13 @@ constexpr bool test() {
     assert(!e2.has_value());
   }
 
+  {
+    // https://github.com/llvm/llvm-project/issues/92676
+    std::expected<Any, int> e1;
+    auto e2 = e1;
+    assert(e2.has_value());
+  }
+
   return true;
 }
 



More information about the libcxx-commits mailing list