[libcxx-commits] [libcxx] [libc++] Cooperation between `std::optional` and other standard types (PR #93672)
via libcxx-commits
libcxx-commits at lists.llvm.org
Wed May 29 05:19:25 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 1e44a9690915e8acf7b2a0e67b56aaf4509e9257 296eed70066767b1f4d84a974885a9ed6a7a3a88 -- libcxx/include/__optional/cooperate.h libcxx/test/libcxx/utilities/optional/cooperate/optional.object/optional.object.observe/bool.pass.cpp libcxx/test/libcxx/utilities/optional/cooperate/optional.object/optional.object.observe/dereference.pass.cpp libcxx/test/libcxx/utilities/optional/cooperate/optional.object/optional.object.swap/swap.pass.cpp libcxx/test/libcxx/utilities/optional/cooperate/optional.object/triviality.pass.cpp libcxx/test/libcxx/utilities/optional/cooperate/optional.object/types.pass.cpp libcxx/test/libcxx/utilities/optional/cooperate/optional_size.pass.cpp libcxx/include/__configuration/abi.h libcxx/include/__functional/reference_wrapper.h libcxx/include/optional
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/libcxx/test/libcxx/utilities/optional/cooperate/optional.object/optional.object.observe/bool.pass.cpp b/libcxx/test/libcxx/utilities/optional/cooperate/optional.object/optional.object.observe/bool.pass.cpp
index 14463b5cfa..f03deda22b 100644
--- a/libcxx/test/libcxx/utilities/optional/cooperate/optional.object/optional.object.observe/bool.pass.cpp
+++ b/libcxx/test/libcxx/utilities/optional/cooperate/optional.object/optional.object.observe/bool.pass.cpp
@@ -20,10 +20,11 @@
using std::optional;
-template<typename T, typename F>
+template <typename T, typename F>
constexpr bool test(F init) {
{
- const optional<T> opt; (void) opt;
+ const optional<T> opt;
+ (void)opt;
ASSERT_NOEXCEPT(bool(opt));
static_assert(!std::is_convertible_v<optional<T>, bool>);
}
diff --git a/libcxx/test/libcxx/utilities/optional/cooperate/optional.object/optional.object.observe/dereference.pass.cpp b/libcxx/test/libcxx/utilities/optional/cooperate/optional.object/optional.object.observe/dereference.pass.cpp
index 1166df7048..d0ffc216ab 100644
--- a/libcxx/test/libcxx/utilities/optional/cooperate/optional.object/optional.object.observe/dereference.pass.cpp
+++ b/libcxx/test/libcxx/utilities/optional/cooperate/optional.object/optional.object.observe/dereference.pass.cpp
@@ -20,10 +20,11 @@
using std::optional;
-template<typename T, typename G>
+template <typename T, typename G>
constexpr auto test(T init, G test) {
{
- optional<T> opt; (void)opt;
+ optional<T> opt;
+ (void)opt;
ASSERT_SAME_TYPE(decltype(*opt), T&);
LIBCPP_STATIC_ASSERT(noexcept(*opt));
}
diff --git a/libcxx/test/libcxx/utilities/optional/cooperate/optional.object/optional.object.swap/swap.pass.cpp b/libcxx/test/libcxx/utilities/optional/cooperate/optional.object/optional.object.swap/swap.pass.cpp
index 521b95ef63..c056ebbe9b 100644
--- a/libcxx/test/libcxx/utilities/optional/cooperate/optional.object/optional.object.swap/swap.pass.cpp
+++ b/libcxx/test/libcxx/utilities/optional/cooperate/optional.object/optional.object.swap/swap.pass.cpp
@@ -22,87 +22,88 @@
using std::optional;
-
-template<typename T>
+template <typename T>
struct equal_to {
- constexpr bool operator()(T& l, T& r) const noexcept {
- return l == r;
- }
+ constexpr bool operator()(T& l, T& r) const noexcept { return l == r; }
};
-template<typename T>
+template <typename T>
struct equal_to<std::reference_wrapper<T>> {
- constexpr bool operator()(std::reference_wrapper<T>& l, std::reference_wrapper<T>& r) const noexcept {
- return std::addressof(l.get()) == std::addressof(r.get());
- }
+ constexpr bool operator()(std::reference_wrapper<T>& l, std::reference_wrapper<T>& r) const noexcept {
+ return std::addressof(l.get()) == std::addressof(r.get());
+ }
};
-template<class T>
+template <class T>
TEST_CONSTEXPR_CXX20 bool check_swap(T left, T right) {
- constexpr equal_to<T> is_equal;
- {
- optional<T> opt1;
- optional<T> opt2;
- static_assert(noexcept(opt1.swap(opt2)) == (std::is_nothrow_move_constructible_v<T> && std::is_nothrow_swappable_v<T>));
- assert(static_cast<bool>(opt1) == false);
- assert(static_cast<bool>(opt2) == false);
- opt1.swap(opt2);
- assert(static_cast<bool>(opt1) == false);
- assert(static_cast<bool>(opt2) == false);
- }
- {
- optional<T> opt1(left);
- optional<T> opt2;
- static_assert(noexcept(opt1.swap(opt2)) == (std::is_nothrow_move_constructible_v<T> && std::is_nothrow_swappable_v<T>));
- assert(static_cast<bool>(opt1) == true);
- assert(is_equal(*opt1, left));
- assert(static_cast<bool>(opt2) == false);
- opt1.swap(opt2);
- assert(static_cast<bool>(opt1) == false);
- assert(static_cast<bool>(opt2) == true);
- assert(is_equal(*opt2, left));
- }
- {
- optional<T> opt1;
- optional<T> opt2(right);
- static_assert(noexcept(opt1.swap(opt2)) == (std::is_nothrow_move_constructible_v<T> && std::is_nothrow_swappable_v<T>));
- assert(static_cast<bool>(opt1) == false);
- assert(static_cast<bool>(opt2) == true);
- assert(is_equal(*opt2, right));
- opt1.swap(opt2);
- assert(static_cast<bool>(opt1) == true);
- assert(is_equal(*opt1, right));
- assert(static_cast<bool>(opt2) == false);
- }
- {
- optional<T> opt1(left);
- optional<T> opt2(right);
- static_assert(noexcept(opt1.swap(opt2)) == (std::is_nothrow_move_constructible_v<T> && std::is_nothrow_swappable_v<T>));
- assert(static_cast<bool>(opt1) == true);
- assert(is_equal(*opt1, left));
- assert(static_cast<bool>(opt2) == true);
- assert(is_equal(*opt2, right));
- opt1.swap(opt2);
- assert(static_cast<bool>(opt1) == true);
- assert(is_equal(*opt1, right));
- assert(static_cast<bool>(opt2) == true);
- assert(is_equal(*opt2, left));
- }
- return true;
+ constexpr equal_to<T> is_equal;
+ {
+ optional<T> opt1;
+ optional<T> opt2;
+ static_assert(
+ noexcept(opt1.swap(opt2)) == (std::is_nothrow_move_constructible_v<T> && std::is_nothrow_swappable_v<T>));
+ assert(static_cast<bool>(opt1) == false);
+ assert(static_cast<bool>(opt2) == false);
+ opt1.swap(opt2);
+ assert(static_cast<bool>(opt1) == false);
+ assert(static_cast<bool>(opt2) == false);
+ }
+ {
+ optional<T> opt1(left);
+ optional<T> opt2;
+ static_assert(
+ noexcept(opt1.swap(opt2)) == (std::is_nothrow_move_constructible_v<T> && std::is_nothrow_swappable_v<T>));
+ assert(static_cast<bool>(opt1) == true);
+ assert(is_equal(*opt1, left));
+ assert(static_cast<bool>(opt2) == false);
+ opt1.swap(opt2);
+ assert(static_cast<bool>(opt1) == false);
+ assert(static_cast<bool>(opt2) == true);
+ assert(is_equal(*opt2, left));
+ }
+ {
+ optional<T> opt1;
+ optional<T> opt2(right);
+ static_assert(
+ noexcept(opt1.swap(opt2)) == (std::is_nothrow_move_constructible_v<T> && std::is_nothrow_swappable_v<T>));
+ assert(static_cast<bool>(opt1) == false);
+ assert(static_cast<bool>(opt2) == true);
+ assert(is_equal(*opt2, right));
+ opt1.swap(opt2);
+ assert(static_cast<bool>(opt1) == true);
+ assert(is_equal(*opt1, right));
+ assert(static_cast<bool>(opt2) == false);
+ }
+ {
+ optional<T> opt1(left);
+ optional<T> opt2(right);
+ static_assert(
+ noexcept(opt1.swap(opt2)) == (std::is_nothrow_move_constructible_v<T> && std::is_nothrow_swappable_v<T>));
+ assert(static_cast<bool>(opt1) == true);
+ assert(is_equal(*opt1, left));
+ assert(static_cast<bool>(opt2) == true);
+ assert(is_equal(*opt2, right));
+ opt1.swap(opt2);
+ assert(static_cast<bool>(opt1) == true);
+ assert(is_equal(*opt1, right));
+ assert(static_cast<bool>(opt2) == true);
+ assert(is_equal(*opt2, left));
+ }
+ return true;
}
int f() noexcept { return 0; }
int g() noexcept { return 0; }
int main(int, char**) {
- int i, j;
- check_swap<std::reference_wrapper<int>>(i, j);
- check_swap<std::reference_wrapper<const int>>(i, j);
- check_swap<std::reference_wrapper<int()>>(f, g);
- check_swap<std::reference_wrapper<int()noexcept>>(f, g);
+ int i, j;
+ check_swap<std::reference_wrapper<int>>(i, j);
+ check_swap<std::reference_wrapper<const int>>(i, j);
+ check_swap<std::reference_wrapper<int()>>(f, g);
+ check_swap<std::reference_wrapper<int() noexcept>>(f, g);
#if TEST_STD_VER > 17
- static int ii, jj;
- static_assert(check_swap<std::reference_wrapper<const int>>(ii, jj));
- static_assert(check_swap<std::reference_wrapper<int()>>(f, g));
+ static int ii, jj;
+ static_assert(check_swap<std::reference_wrapper<const int>>(ii, jj));
+ static_assert(check_swap<std::reference_wrapper<int()>>(f, g));
#endif
}
diff --git a/libcxx/test/libcxx/utilities/optional/cooperate/optional.object/triviality.pass.cpp b/libcxx/test/libcxx/utilities/optional/cooperate/optional.object/triviality.pass.cpp
index 6563667ed7..5b18e0fbc0 100644
--- a/libcxx/test/libcxx/utilities/optional/cooperate/optional.object/triviality.pass.cpp
+++ b/libcxx/test/libcxx/utilities/optional/cooperate/optional.object/triviality.pass.cpp
@@ -18,7 +18,6 @@
// constexpr optional<T>& operator=(const optional& rhs);
// constexpr optional<T>& operator=(optional&& rhs) noexcept(see below);
-
#include <optional>
#include <functional>
#include <type_traits>
@@ -27,55 +26,47 @@
#include "test_macros.h"
-
-constexpr bool implies(bool p, bool q) {
- return !p || q;
-}
+constexpr bool implies(bool p, bool q) { return !p || q; }
template <class T>
struct SpecialMemberTest {
- using O = std::optional<T>;
-
- static_assert(implies(std::is_trivially_copy_constructible_v<T>,
- std::is_trivially_copy_constructible_v<O>),
- "optional<T> is trivially copy constructible if T is trivially copy constructible.");
-
- static_assert(implies(std::is_trivially_move_constructible_v<T>,
- std::is_trivially_move_constructible_v<O>),
- "optional<T> is trivially move constructible if T is trivially move constructible");
-
- static_assert(implies(std::is_trivially_copy_constructible_v<T> &&
- std::is_trivially_copy_assignable_v<T> &&
- std::is_trivially_destructible_v<T>,
-
- std::is_trivially_copy_assignable_v<O>),
- "optional<T> is trivially copy assignable if T is "
- "trivially copy constructible, "
- "trivially copy assignable, and "
- "trivially destructible");
-
- static_assert(implies(std::is_trivially_move_constructible_v<T> &&
- std::is_trivially_move_assignable_v<T> &&
- std::is_trivially_destructible_v<T>,
-
- std::is_trivially_move_assignable_v<O>),
- "optional<T> is trivially move assignable if T is "
- "trivially move constructible, "
- "trivially move assignable, and"
- "trivially destructible.");
+ using O = std::optional<T>;
+
+ static_assert(implies(std::is_trivially_copy_constructible_v<T>, std::is_trivially_copy_constructible_v<O>),
+ "optional<T> is trivially copy constructible if T is trivially copy constructible.");
+
+ static_assert(implies(std::is_trivially_move_constructible_v<T>, std::is_trivially_move_constructible_v<O>),
+ "optional<T> is trivially move constructible if T is trivially move constructible");
+
+ static_assert(implies(std::is_trivially_copy_constructible_v<T> && std::is_trivially_copy_assignable_v<T> &&
+ std::is_trivially_destructible_v<T>,
+
+ std::is_trivially_copy_assignable_v<O>),
+ "optional<T> is trivially copy assignable if T is "
+ "trivially copy constructible, "
+ "trivially copy assignable, and "
+ "trivially destructible");
+
+ static_assert(implies(std::is_trivially_move_constructible_v<T> && std::is_trivially_move_assignable_v<T> &&
+ std::is_trivially_destructible_v<T>,
+
+ std::is_trivially_move_assignable_v<O>),
+ "optional<T> is trivially move assignable if T is "
+ "trivially move constructible, "
+ "trivially move assignable, and"
+ "trivially destructible.");
};
-template <class ...Args> static void sink(Args&&...) {}
+template <class... Args>
+static void sink(Args&&...) {}
-template <class ...TestTypes>
+template <class... TestTypes>
struct DoTestsMetafunction {
- DoTestsMetafunction() { sink(SpecialMemberTest<TestTypes>{}...); }
+ DoTestsMetafunction() { sink(SpecialMemberTest<TestTypes>{}...); }
};
int main(int, char**) {
- DoTestsMetafunction<
- std::reference_wrapper<int>,
- std::reference_wrapper<int()>,
- std::reference_wrapper<int() noexcept>
- >{};
+ DoTestsMetafunction< std::reference_wrapper<int>,
+ std::reference_wrapper<int()>,
+ std::reference_wrapper<int() noexcept> >{};
}
diff --git a/libcxx/test/libcxx/utilities/optional/cooperate/optional.object/types.pass.cpp b/libcxx/test/libcxx/utilities/optional/cooperate/optional.object/types.pass.cpp
index 89313cb7b4..d012e9157f 100644
--- a/libcxx/test/libcxx/utilities/optional/cooperate/optional.object/types.pass.cpp
+++ b/libcxx/test/libcxx/utilities/optional/cooperate/optional.object/types.pass.cpp
@@ -25,12 +25,8 @@
using std::optional;
template <class Opt, class T>
-void
-test()
-{
- static_assert(std::is_same<typename Opt::value_type, T>::value, "");
+void test() {
+ static_assert(std::is_same<typename Opt::value_type, T>::value, "");
}
-int main(int, char**) {
- test<optional<std::reference_wrapper<int>>, std::reference_wrapper<int>>();
-}
+int main(int, char**) { test<optional<std::reference_wrapper<int>>, std::reference_wrapper<int>>(); }
``````````
</details>
https://github.com/llvm/llvm-project/pull/93672
More information about the libcxx-commits
mailing list