[libcxx-commits] [clang-tools-extra] [compiler-rt] [libc] [lldb] [llvm] [libcxx] [clang] [flang] [libc++][variant] P2637R3: Member `visit` (`std::variant`) (PR #76447)

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Dec 29 22:27:46 PST 2023


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 886655869cef2e0f11da8981da30d70ad7892ff9 4d3ff8bd92f92404db1d1dd56e8ff4a5b5226dbb -- libcxx/test/std/utilities/variant/variant.visit/member.visit.pass.cpp libcxx/test/std/utilities/variant/variant.visit/member.visit.robust_against_adl.pass.cpp libcxx/test/std/utilities/variant/variant.visit/member.visit_return_type.pass.cpp libcxx/include/variant libcxx/test/std/utilities/variant/variant.visit/visit.return_type.pass.cpp libcxx/test/std/utilities/variant/variant.visit/visit.robust_against_adl.pass.cpp
``````````

</details>

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

``````````diff
diff --git a/libcxx/test/std/utilities/variant/variant.visit/visit.return_type.pass.cpp b/libcxx/test/std/utilities/variant/variant.visit/visit.return_type.pass.cpp
index eb425c07f9..246b01a68f 100644
--- a/libcxx/test/std/utilities/variant/variant.visit/visit.return_type.pass.cpp
+++ b/libcxx/test/std/utilities/variant/variant.visit/visit.return_type.pass.cpp
@@ -26,7 +26,7 @@ template <typename ReturnType>
 void test_call_operator_forwarding() {
   using Fn = ForwardingCallObject;
   Fn obj{};
-  const Fn &cobj = obj;
+  const Fn& cobj = obj;
   { // test call operator forwarding - no variant
     std::visit<ReturnType>(obj);
     assert(Fn::check_call<>(CT_NonConst | CT_LValue));
@@ -41,63 +41,63 @@ void test_call_operator_forwarding() {
     using V = std::variant<int>;
     V v(42);
     std::visit<ReturnType>(obj, v);
-    assert(Fn::check_call<int &>(CT_NonConst | CT_LValue));
+    assert(Fn::check_call<int&>(CT_NonConst | CT_LValue));
     std::visit<ReturnType>(cobj, v);
-    assert(Fn::check_call<int &>(CT_Const | CT_LValue));
+    assert(Fn::check_call<int&>(CT_Const | CT_LValue));
     std::visit<ReturnType>(std::move(obj), v);
-    assert(Fn::check_call<int &>(CT_NonConst | CT_RValue));
+    assert(Fn::check_call<int&>(CT_NonConst | CT_RValue));
     std::visit<ReturnType>(std::move(cobj), v);
-    assert(Fn::check_call<int &>(CT_Const | CT_RValue));
+    assert(Fn::check_call<int&>(CT_Const | CT_RValue));
   }
   { // test call operator forwarding - single variant, multi arg
     using V = std::variant<int, long, double>;
     V v(42l);
     std::visit<ReturnType>(obj, v);
-    assert(Fn::check_call<long &>(CT_NonConst | CT_LValue));
+    assert(Fn::check_call<long&>(CT_NonConst | CT_LValue));
     std::visit<ReturnType>(cobj, v);
-    assert(Fn::check_call<long &>(CT_Const | CT_LValue));
+    assert(Fn::check_call<long&>(CT_Const | CT_LValue));
     std::visit<ReturnType>(std::move(obj), v);
-    assert(Fn::check_call<long &>(CT_NonConst | CT_RValue));
+    assert(Fn::check_call<long&>(CT_NonConst | CT_RValue));
     std::visit<ReturnType>(std::move(cobj), v);
-    assert(Fn::check_call<long &>(CT_Const | CT_RValue));
+    assert(Fn::check_call<long&>(CT_Const | CT_RValue));
   }
   { // test call operator forwarding - multi variant, multi arg
-    using V = std::variant<int, long, double>;
-    using V2 = std::variant<int *, std::string>;
+    using V  = std::variant<int, long, double>;
+    using V2 = std::variant<int*, std::string>;
     V v(42l);
     V2 v2("hello");
     std::visit<int>(obj, v, v2);
-    assert((Fn::check_call<long &, std::string &>(CT_NonConst | CT_LValue)));
+    assert((Fn::check_call<long&, std::string&>(CT_NonConst | CT_LValue)));
     std::visit<ReturnType>(cobj, v, v2);
-    assert((Fn::check_call<long &, std::string &>(CT_Const | CT_LValue)));
+    assert((Fn::check_call<long&, std::string&>(CT_Const | CT_LValue)));
     std::visit<ReturnType>(std::move(obj), v, v2);
-    assert((Fn::check_call<long &, std::string &>(CT_NonConst | CT_RValue)));
+    assert((Fn::check_call<long&, std::string&>(CT_NonConst | CT_RValue)));
     std::visit<ReturnType>(std::move(cobj), v, v2);
-    assert((Fn::check_call<long &, std::string &>(CT_Const | CT_RValue)));
+    assert((Fn::check_call<long&, std::string&>(CT_Const | CT_RValue)));
   }
   {
     using V = std::variant<int, long, double, std::string>;
     V v1(42l), v2("hello"), v3(101), v4(1.1);
     std::visit<ReturnType>(obj, v1, v2, v3, v4);
-    assert((Fn::check_call<long &, std::string &, int &, double &>(CT_NonConst | CT_LValue)));
+    assert((Fn::check_call<long&, std::string&, int&, double&>(CT_NonConst | CT_LValue)));
     std::visit<ReturnType>(cobj, v1, v2, v3, v4);
-    assert((Fn::check_call<long &, std::string &, int &, double &>(CT_Const | CT_LValue)));
+    assert((Fn::check_call<long&, std::string&, int&, double&>(CT_Const | CT_LValue)));
     std::visit<ReturnType>(std::move(obj), v1, v2, v3, v4);
-    assert((Fn::check_call<long &, std::string &, int &, double &>(CT_NonConst | CT_RValue)));
+    assert((Fn::check_call<long&, std::string&, int&, double&>(CT_NonConst | CT_RValue)));
     std::visit<ReturnType>(std::move(cobj), v1, v2, v3, v4);
-    assert((Fn::check_call<long &, std::string &, int &, double &>(CT_Const | CT_RValue)));
+    assert((Fn::check_call<long&, std::string&, int&, double&>(CT_Const | CT_RValue)));
   }
   {
     using V = std::variant<int, long, double, int*, std::string>;
     V v1(42l), v2("hello"), v3(nullptr), v4(1.1);
     std::visit<ReturnType>(obj, v1, v2, v3, v4);
-    assert((Fn::check_call<long &, std::string &, int *&, double &>(CT_NonConst | CT_LValue)));
+    assert((Fn::check_call<long&, std::string&, int*&, double&>(CT_NonConst | CT_LValue)));
     std::visit<ReturnType>(cobj, v1, v2, v3, v4);
-    assert((Fn::check_call<long &, std::string &, int *&, double &>(CT_Const | CT_LValue)));
+    assert((Fn::check_call<long&, std::string&, int*&, double&>(CT_Const | CT_LValue)));
     std::visit<ReturnType>(std::move(obj), v1, v2, v3, v4);
-    assert((Fn::check_call<long &, std::string &, int *&, double &>(CT_NonConst | CT_RValue)));
+    assert((Fn::check_call<long&, std::string&, int*&, double&>(CT_NonConst | CT_RValue)));
     std::visit<ReturnType>(std::move(cobj), v1, v2, v3, v4);
-    assert((Fn::check_call<long &, std::string &, int *&, double &>(CT_Const | CT_RValue)));
+    assert((Fn::check_call<long&, std::string&, int*&, double&>(CT_Const | CT_RValue)));
   }
 }
 
@@ -109,69 +109,69 @@ void test_argument_forwarding() {
   { // single argument - value type
     using V = std::variant<int>;
     V v(42);
-    const V &cv = v;
+    const V& cv = v;
     std::visit<ReturnType>(obj, v);
-    assert(Fn::check_call<int &>(Val));
+    assert(Fn::check_call<int&>(Val));
     std::visit<ReturnType>(obj, cv);
-    assert(Fn::check_call<const int &>(Val));
+    assert(Fn::check_call<const int&>(Val));
     std::visit<ReturnType>(obj, std::move(v));
-    assert(Fn::check_call<int &&>(Val));
+    assert(Fn::check_call<int&&>(Val));
     std::visit<ReturnType>(obj, std::move(cv));
-    assert(Fn::check_call<const int &&>(Val));
+    assert(Fn::check_call<const int&&>(Val));
   }
 #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
   { // single argument - lvalue reference
-    using V = std::variant<int &>;
-    int x = 42;
+    using V = std::variant<int&>;
+    int x   = 42;
     V v(x);
-    const V &cv = v;
+    const V& cv = v;
     std::visit<ReturnType>(obj, v);
-    assert(Fn::check_call<int &>(Val));
+    assert(Fn::check_call<int&>(Val));
     std::visit<ReturnType>(obj, cv);
-    assert(Fn::check_call<int &>(Val));
+    assert(Fn::check_call<int&>(Val));
     std::visit<ReturnType>(obj, std::move(v));
-    assert(Fn::check_call<int &>(Val));
+    assert(Fn::check_call<int&>(Val));
     std::visit<ReturnType>(obj, std::move(cv));
-    assert(Fn::check_call<int &>(Val));
+    assert(Fn::check_call<int&>(Val));
   }
   { // single argument - rvalue reference
-    using V = std::variant<int &&>;
-    int x = 42;
+    using V = std::variant<int&&>;
+    int x   = 42;
     V v(std::move(x));
-    const V &cv = v;
+    const V& cv = v;
     std::visit<ReturnType>(obj, v);
-    assert(Fn::check_call<int &>(Val));
+    assert(Fn::check_call<int&>(Val));
     std::visit<ReturnType>(obj, cv);
-    assert(Fn::check_call<int &>(Val));
+    assert(Fn::check_call<int&>(Val));
     std::visit<ReturnType>(obj, std::move(v));
-    assert(Fn::check_call<int &&>(Val));
+    assert(Fn::check_call<int&&>(Val));
     std::visit<ReturnType>(obj, std::move(cv));
-    assert(Fn::check_call<int &&>(Val));
+    assert(Fn::check_call<int&&>(Val));
   }
 #endif
   { // multi argument - multi variant
     using V = std::variant<int, std::string, long>;
     V v1(42), v2("hello"), v3(43l);
     std::visit<ReturnType>(obj, v1, v2, v3);
-    assert((Fn::check_call<int &, std::string &, long &>(Val)));
+    assert((Fn::check_call<int&, std::string&, long&>(Val)));
     std::visit<ReturnType>(obj, std::as_const(v1), std::as_const(v2), std::move(v3));
-    assert((Fn::check_call<const int &, const std::string &, long &&>(Val)));
+    assert((Fn::check_call<const int&, const std::string&, long&&>(Val)));
   }
   {
     using V = std::variant<int, long, double, std::string>;
     V v1(42l), v2("hello"), v3(101), v4(1.1);
     std::visit<ReturnType>(obj, v1, v2, v3, v4);
-    assert((Fn::check_call<long &, std::string &, int &, double &>(Val)));
+    assert((Fn::check_call<long&, std::string&, int&, double&>(Val)));
     std::visit<ReturnType>(obj, std::as_const(v1), std::as_const(v2), std::move(v3), std::move(v4));
-    assert((Fn::check_call<const long &, const std::string &, int &&, double &&>(Val)));
+    assert((Fn::check_call<const long&, const std::string&, int&&, double&&>(Val)));
   }
   {
     using V = std::variant<int, long, double, int*, std::string>;
     V v1(42l), v2("hello"), v3(nullptr), v4(1.1);
     std::visit<ReturnType>(obj, v1, v2, v3, v4);
-    assert((Fn::check_call<long &, std::string &, int *&, double &>(Val)));
+    assert((Fn::check_call<long&, std::string&, int*&, double&>(Val)));
     std::visit<ReturnType>(obj, std::as_const(v1), std::as_const(v2), std::move(v3), std::move(v4));
-    assert((Fn::check_call<const long &, const std::string &, int *&&, double &&>(Val)));
+    assert((Fn::check_call<const long&, const std::string&, int*&&, double&&>(Val)));
   }
 }
 
@@ -179,7 +179,7 @@ template <typename ReturnType>
 void test_return_type() {
   using Fn = ForwardingCallObject;
   Fn obj{};
-  const Fn &cobj = obj;
+  const Fn& cobj = obj;
   { // test call operator forwarding - no variant
     static_assert(std::is_same_v<decltype(std::visit<ReturnType>(obj)), ReturnType>);
     static_assert(std::is_same_v<decltype(std::visit<ReturnType>(cobj)), ReturnType>);
@@ -203,8 +203,8 @@ void test_return_type() {
     static_assert(std::is_same_v<decltype(std::visit<ReturnType>(std::move(cobj), v)), ReturnType>);
   }
   { // test call operator forwarding - multi variant, multi arg
-    using V = std::variant<int, long, double>;
-    using V2 = std::variant<int *, std::string>;
+    using V  = std::variant<int, long, double>;
+    using V2 = std::variant<int*, std::string>;
     V v(42l);
     V2 v2("hello");
     static_assert(std::is_same_v<decltype(std::visit<ReturnType>(obj, v, v2)), ReturnType>);
@@ -245,7 +245,7 @@ void test_constexpr_void() {
   }
   {
     using V1 = std::variant<int>;
-    using V2 = std::variant<int, char *, long long>;
+    using V2 = std::variant<int, char*, long long>;
     using V3 = std::variant<bool, int, int>;
     constexpr V1 v1;
     constexpr V2 v2(nullptr);
@@ -254,20 +254,20 @@ void test_constexpr_void() {
   }
   {
     using V1 = std::variant<int>;
-    using V2 = std::variant<int, char *, long long>;
-    using V3 = std::variant<void *, int, int>;
+    using V2 = std::variant<int, char*, long long>;
+    using V3 = std::variant<void*, int, int>;
     constexpr V1 v1;
     constexpr V2 v2(nullptr);
     constexpr V3 v3;
     static_assert((std::visit<void>(aobj, v1, v2, v3), 3) == 3, "");
   }
   {
-    using V = std::variant<int, long, double, int *>;
+    using V = std::variant<int, long, double, int*>;
     constexpr V v1(42l), v2(101), v3(nullptr), v4(1.1);
     static_assert((std::visit<void>(aobj, v1, v2, v3, v4), 4) == 4, "");
   }
   {
-    using V = std::variant<int, long, double, long long, int *>;
+    using V = std::variant<int, long, double, long long, int*>;
     constexpr V v1(42l), v2(101), v3(nullptr), v4(1.1);
     static_assert((std::visit<void>(aobj, v1, v2, v3, v4), 4) == 4, "");
   }
@@ -288,7 +288,7 @@ void test_constexpr_int() {
   }
   {
     using V1 = std::variant<int>;
-    using V2 = std::variant<int, char *, long long>;
+    using V2 = std::variant<int, char*, long long>;
     using V3 = std::variant<bool, int, int>;
     constexpr V1 v1;
     constexpr V2 v2(nullptr);
@@ -297,20 +297,20 @@ void test_constexpr_int() {
   }
   {
     using V1 = std::variant<int>;
-    using V2 = std::variant<int, char *, long long>;
-    using V3 = std::variant<void *, int, int>;
+    using V2 = std::variant<int, char*, long long>;
+    using V3 = std::variant<void*, int, int>;
     constexpr V1 v1;
     constexpr V2 v2(nullptr);
     constexpr V3 v3;
     static_assert(std::visit<int>(aobj, v1, v2, v3) == 3, "");
   }
   {
-    using V = std::variant<int, long, double, int *>;
+    using V = std::variant<int, long, double, int*>;
     constexpr V v1(42l), v2(101), v3(nullptr), v4(1.1);
     static_assert(std::visit<int>(aobj, v1, v2, v3, v4) == 4, "");
   }
   {
-    using V = std::variant<int, long, double, long long, int *>;
+    using V = std::variant<int, long, double, long long, int*>;
     constexpr V v1(42l), v2(101), v3(nullptr), v4(1.1);
     static_assert(std::visit<int>(aobj, v1, v2, v3, v4) == 4, "");
   }
@@ -320,10 +320,10 @@ template <typename ReturnType>
 void test_exceptions() {
 #ifndef TEST_HAS_NO_EXCEPTIONS
   ReturnArity obj{};
-  auto test = [&](auto &&... args) {
+  auto test = [&](auto&&... args) {
     try {
       std::visit<ReturnType>(obj, args...);
-    } catch (const std::bad_variant_access &) {
+    } catch (const std::bad_variant_access&) {
       return true;
     } catch (...) {
     }
@@ -336,24 +336,24 @@ void test_exceptions() {
     assert(test(v));
   }
   {
-    using V = std::variant<int, MakeEmptyT>;
-    using V2 = std::variant<long, std::string, void *>;
+    using V  = std::variant<int, MakeEmptyT>;
+    using V2 = std::variant<long, std::string, void*>;
     V v;
     makeEmpty(v);
     V2 v2("hello");
     assert(test(v, v2));
   }
   {
-    using V = std::variant<int, MakeEmptyT>;
-    using V2 = std::variant<long, std::string, void *>;
+    using V  = std::variant<int, MakeEmptyT>;
+    using V2 = std::variant<long, std::string, void*>;
     V v;
     makeEmpty(v);
     V2 v2("hello");
     assert(test(v2, v));
   }
   {
-    using V = std::variant<int, MakeEmptyT>;
-    using V2 = std::variant<long, std::string, void *, MakeEmptyT>;
+    using V  = std::variant<int, MakeEmptyT>;
+    using V2 = std::variant<long, std::string, void*, MakeEmptyT>;
     V v;
     makeEmpty(v);
     V2 v2;
@@ -384,8 +384,7 @@ void test_caller_accepts_nonconst() {
   struct A {};
   struct Visitor {
     auto operator()(A&) {
-      if constexpr (!std::is_void_v<ReturnType>)
-      {
+      if constexpr (!std::is_void_v<ReturnType>) {
         return ReturnType{};
       }
     }
@@ -426,9 +425,7 @@ void test_derived_from_variant() {
     char valueless_by_exception;
   };
 
-  struct EvilVariant1 : std::variant<int, long, double>,
-                        std::tuple<int>,
-                        EvilVariantBase {
+  struct EvilVariant1 : std::variant<int, long, double>, std::tuple<int>, EvilVariantBase {
     using std::variant<int, long, double>::variant;
   };
 
@@ -448,7 +445,10 @@ void test_derived_from_variant() {
   // Check that visit unambiguously picks the variant, even if the other base has __impl member.
   struct ImplVariantBase {
     struct Callable {
-      bool operator()() const { assert(false); return false; }
+      bool operator()() const {
+        assert(false);
+        return false;
+      }
     };
 
     Callable __impl;
@@ -479,8 +479,7 @@ struct any_visitor {
   }
 };
 
-template <typename T, typename = decltype(std::visit<bool>(
-                          std::declval<any_visitor&>(), std::declval<T>()))>
+template <typename T, typename = decltype(std::visit<bool>(std::declval<any_visitor&>(), std::declval<T>()))>
 constexpr bool has_visit(int) {
   return true;
 }
diff --git a/libcxx/test/std/utilities/variant/variant.visit/visit.robust_against_adl.pass.cpp b/libcxx/test/std/utilities/variant/variant.visit/visit.robust_against_adl.pass.cpp
index 6f17fa3264..125ac20490 100644
--- a/libcxx/test/std/utilities/variant/variant.visit/visit.robust_against_adl.pass.cpp
+++ b/libcxx/test/std/utilities/variant/variant.visit/visit.robust_against_adl.pass.cpp
@@ -17,27 +17,28 @@
 #include "test_macros.h"
 
 struct Incomplete;
-template<class T> struct Holder { T t; };
+template <class T>
+struct Holder {
+  T t;
+};
 
-constexpr bool test(bool do_it)
-{
-    if (do_it) {
-        std::variant<Holder<Incomplete>*, int> v = nullptr;
-        std::visit([](auto){}, v);
-        std::visit([](auto) -> Holder<Incomplete>* { return nullptr; }, v);
+constexpr bool test(bool do_it) {
+  if (do_it) {
+    std::variant<Holder<Incomplete>*, int> v = nullptr;
+    std::visit([](auto) {}, v);
+    std::visit([](auto) -> Holder<Incomplete>* { return nullptr; }, v);
 #if TEST_STD_VER > 17
-        std::visit<void>([](auto){}, v);
-        std::visit<void*>([](auto) -> Holder<Incomplete>* { return nullptr; }, v);
+    std::visit<void>([](auto) {}, v);
+    std::visit<void*>([](auto) -> Holder<Incomplete>* { return nullptr; }, v);
 #endif
-    }
-    return true;
+  }
+  return true;
 }
 
-int main(int, char**)
-{
-    test(true);
+int main(int, char**) {
+  test(true);
 #if TEST_STD_VER > 17
-    static_assert(test(true));
+  static_assert(test(true));
 #endif
-    return 0;
+  return 0;
 }

``````````

</details>


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


More information about the libcxx-commits mailing list