[libcxx-commits] [libcxx] [libc++] Make `constexpr std::variant`. Implement P2231R1 (PR #83335)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Fri Mar 8 08:38:23 PST 2024


================
@@ -201,35 +199,17 @@ void test_T_assignment_basic() {
 #endif
   {
     std::variant<std::string, bool> v = true;
-    v = "bar";
+    v                                 = "bar";
     assert(v.index() == 0);
     assert(std::get<0>(v) == "bar");
   }
-  {
-    std::variant<bool, std::unique_ptr<int>> v;
-    v = nullptr;
-    assert(v.index() == 1);
-    assert(std::get<1>(v) == nullptr);
-  }
-#if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
-  {
-    using V = std::variant<int &, int &&, long>;
-    int x = 42;
-    V v(43l);
-    v = x;
-    assert(v.index() == 0);
-    assert(&std::get<0>(v) == &x);
-    v = std::move(x);
-    assert(v.index() == 1);
-    assert(&std::get<1>(v) == &x);
-    // 'long' is selected by FUN(const int &) since 'const int &' cannot bind
-    // to 'int&'.
-    const int &cx = x;
-    v = cx;
-    assert(v.index() == 2);
-    assert(std::get<2>(v) == 42);
-  }
-#endif // TEST_VARIANT_HAS_NO_REFERENCES
+}
+
+void test_T_assignment_basic_no_constexpr() {
+  std::variant<bool, std::unique_ptr<int>> v;
+  v = nullptr;
+  assert(v.index() == 1);
+  assert(std::get<1>(v) == nullptr);
 }
 
 void test_T_assignment_performs_construction() {
----------------
ldionne wrote:

My understanding of this test is that it's just trying to validate that we call the constructor-from-type-T when we assign to a variant, and it's simply using exceptions to signal that the right thing happened. In other words, this test is not fundamentally tied to exceptions, it's just a means to an end.

As you just suggested, we could instead assign from a pointer and set something in the pointer to signal that we've been constructed as expected. That would be constexpr-friendly.

https://github.com/llvm/llvm-project/pull/83335


More information about the libcxx-commits mailing list