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

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Mar 27 00:43:33 PDT 2024


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff 6d13263d4a723689d025423562269ea6ccb6bfc2 b54f8a1924cb7c8d7142d2cef75aab9716319c6c -- libcxx/include/variant libcxx/test/std/utilities/variant/variant.variant/variant.assign/T.pass.cpp libcxx/test/std/utilities/variant/variant.variant/variant.assign/copy.pass.cpp libcxx/test/std/utilities/variant/variant.variant/variant.assign/move.pass.cpp libcxx/test/std/utilities/variant/variant.variant/variant.ctor/copy.pass.cpp libcxx/test/std/utilities/variant/variant.variant/variant.ctor/move.pass.cpp libcxx/test/std/utilities/variant/variant.variant/variant.dtor/dtor.pass.cpp libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_index_args.pass.cpp libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_index_init_list_args.pass.cpp libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_type_args.pass.cpp libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_type_init_list_args.pass.cpp libcxx/test/std/utilities/variant/variant.variant/variant.swap/swap.pass.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_index_args.pass.cpp b/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_index_args.pass.cpp
index f98d968f0e..f813b3f0a1 100644
--- a/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_index_args.pass.cpp
+++ b/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_index_args.pass.cpp
@@ -26,8 +26,8 @@
 #include "variant_test_helpers.h"
 
 template <class Var, std::size_t I, class... Args>
-constexpr auto test_emplace_exists_imp(int)
-    -> decltype(std::declval<Var>().template emplace<I>(std::declval<Args>()...), true) {
+constexpr auto
+test_emplace_exists_imp(int) -> decltype(std::declval<Var>().template emplace<I>(std::declval<Args>()...), true) {
   return true;
 }
 
diff --git a/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_index_init_list_args.pass.cpp b/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_index_init_list_args.pass.cpp
index 4c635570bd..f14092d145 100644
--- a/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_index_init_list_args.pass.cpp
+++ b/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_index_init_list_args.pass.cpp
@@ -36,8 +36,8 @@ struct InitListArg {
 };
 
 template <class Var, std::size_t I, class... Args>
-constexpr auto test_emplace_exists_imp(int)
-    -> decltype(std::declval<Var>().template emplace<I>(std::declval<Args>()...), true) {
+constexpr auto
+test_emplace_exists_imp(int) -> decltype(std::declval<Var>().template emplace<I>(std::declval<Args>()...), true) {
   return true;
 }
 
diff --git a/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_type_args.pass.cpp b/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_type_args.pass.cpp
index c2ed54d8a6..2c91debfa1 100644
--- a/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_type_args.pass.cpp
+++ b/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_type_args.pass.cpp
@@ -25,8 +25,8 @@
 #include "variant_test_helpers.h"
 
 template <class Var, class T, class... Args>
-constexpr auto test_emplace_exists_imp(int)
-    -> decltype(std::declval<Var>().template emplace<T>(std::declval<Args>()...), true) {
+constexpr auto
+test_emplace_exists_imp(int) -> decltype(std::declval<Var>().template emplace<T>(std::declval<Args>()...), true) {
   return true;
 }
 
diff --git a/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_type_init_list_args.pass.cpp b/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_type_init_list_args.pass.cpp
index 644f2418b9..a442c7ed5d 100644
--- a/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_type_init_list_args.pass.cpp
+++ b/libcxx/test/std/utilities/variant/variant.variant/variant.mod/emplace_type_init_list_args.pass.cpp
@@ -36,8 +36,8 @@ struct InitListArg {
 };
 
 template <class Var, class T, class... Args>
-constexpr auto test_emplace_exists_imp(int)
-    -> decltype(std::declval<Var>().template emplace<T>(std::declval<Args>()...), true) {
+constexpr auto
+test_emplace_exists_imp(int) -> decltype(std::declval<Var>().template emplace<T>(std::declval<Args>()...), true) {
   return true;
 }
 
diff --git a/libcxx/test/std/utilities/variant/variant.variant/variant.swap/swap.pass.cpp b/libcxx/test/std/utilities/variant/variant.variant/variant.swap/swap.pass.cpp
index db05691c55..7269eae98e 100644
--- a/libcxx/test/std/utilities/variant/variant.variant/variant.swap/swap.pass.cpp
+++ b/libcxx/test/std/utilities/variant/variant.variant/variant.swap/swap.pass.cpp
@@ -251,62 +251,63 @@ TEST_CONSTEXPR_CXX20 void test_swap_same_alternative() {
   }
 }
 
-void test_swap_same_alternative_throws(){
+void test_swap_same_alternative_throws() {
 #ifndef TEST_HAS_NO_EXCEPTIONS
-    {using V = std::variant<NothrowTypeWithThrowingSwap, int>;
-int move_called        = 0;
-int move_assign_called = 0;
-int swap_called        = 0;
-V v1(std::in_place_index<0>, 42, &move_called, &move_assign_called, &swap_called);
-V v2(std::in_place_index<0>, 100, &move_called, &move_assign_called, &swap_called);
-try {
-  v1.swap(v2);
-  assert(false);
-} catch (int) {
-}
-assert(swap_called == 1);
-assert(move_called == 0);
-assert(move_assign_called == 0);
-assert(std::get<0>(v1).value == 42);
-assert(std::get<0>(v2).value == 100);
-}
+  {
+    using V                = std::variant<NothrowTypeWithThrowingSwap, int>;
+    int move_called        = 0;
+    int move_assign_called = 0;
+    int swap_called        = 0;
+    V v1(std::in_place_index<0>, 42, &move_called, &move_assign_called, &swap_called);
+    V v2(std::in_place_index<0>, 100, &move_called, &move_assign_called, &swap_called);
+    try {
+      v1.swap(v2);
+      assert(false);
+    } catch (int) {
+    }
+    assert(swap_called == 1);
+    assert(move_called == 0);
+    assert(move_assign_called == 0);
+    assert(std::get<0>(v1).value == 42);
+    assert(std::get<0>(v2).value == 100);
+  }
 
-{
-  using V                = std::variant<ThrowingMoveCtor, int>;
-  int move_called        = 0;
-  int move_assign_called = 0;
-  int swap_called        = 0;
-  V v1(std::in_place_index<0>, 42, &move_called, &move_assign_called, &swap_called);
-  V v2(std::in_place_index<0>, 100, &move_called, &move_assign_called, &swap_called);
-  try {
-    v1.swap(v2);
-    assert(false);
-  } catch (int) {
+  {
+    using V                = std::variant<ThrowingMoveCtor, int>;
+    int move_called        = 0;
+    int move_assign_called = 0;
+    int swap_called        = 0;
+    V v1(std::in_place_index<0>, 42, &move_called, &move_assign_called, &swap_called);
+    V v2(std::in_place_index<0>, 100, &move_called, &move_assign_called, &swap_called);
+    try {
+      v1.swap(v2);
+      assert(false);
+    } catch (int) {
+    }
+    assert(move_called == 1); // call threw
+    assert(move_assign_called == 0);
+    assert(swap_called == 0);
+    assert(std::get<0>(v1).value == 42); // throw happened before v1 was moved from
+    assert(std::get<0>(v2).value == 100);
   }
-  assert(move_called == 1); // call threw
-  assert(move_assign_called == 0);
-  assert(swap_called == 0);
-  assert(std::get<0>(v1).value == 42); // throw happened before v1 was moved from
-  assert(std::get<0>(v2).value == 100);
-}
-{
-  using V                = std::variant<ThrowingMoveAssignNothrowMoveCtor, int>;
-  int move_called        = 0;
-  int move_assign_called = 0;
-  int swap_called        = 0;
-  V v1(std::in_place_index<0>, 42, &move_called, &move_assign_called, &swap_called);
-  V v2(std::in_place_index<0>, 100, &move_called, &move_assign_called, &swap_called);
-  try {
-    v1.swap(v2);
-    assert(false);
-  } catch (int) {
+  {
+    using V                = std::variant<ThrowingMoveAssignNothrowMoveCtor, int>;
+    int move_called        = 0;
+    int move_assign_called = 0;
+    int swap_called        = 0;
+    V v1(std::in_place_index<0>, 42, &move_called, &move_assign_called, &swap_called);
+    V v2(std::in_place_index<0>, 100, &move_called, &move_assign_called, &swap_called);
+    try {
+      v1.swap(v2);
+      assert(false);
+    } catch (int) {
+    }
+    assert(move_called == 1);
+    assert(move_assign_called == 1); // call threw and didn't complete
+    assert(swap_called == 0);
+    assert(std::get<0>(v1).value == -1); // v1 was moved from
+    assert(std::get<0>(v2).value == 100);
   }
-  assert(move_called == 1);
-  assert(move_assign_called == 1); // call threw and didn't complete
-  assert(swap_called == 0);
-  assert(std::get<0>(v1).value == -1); // v1 was moved from
-  assert(std::get<0>(v2).value == 100);
-}
 #endif
 }
 

``````````

</details>


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


More information about the libcxx-commits mailing list