[libcxx-commits] [libcxx] dc9f385 - [libcxx] adds concept std::movable
Christopher Di Bella via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Mar 11 19:29:00 PST 2021
Author: Christopher Di Bella
Date: 2021-03-11T19:27:43-08:00
New Revision: dc9f385722245858a2d5160d5764158195e3751e
URL: https://github.com/llvm/llvm-project/commit/dc9f385722245858a2d5160d5764158195e3751e
DIFF: https://github.com/llvm/llvm-project/commit/dc9f385722245858a2d5160d5764158195e3751e.diff
LOG: [libcxx] adds concept std::movable
Implements parts of:
- P0898R3 Standard Library Concepts
- P1754 Rename concepts to standard_case for C++20, while we still can
Depends on D97162
Reviewed By: EricWF, #libc, Quuxplusone
Differential Revision: https://reviews.llvm.org/D97359
Added:
libcxx/test/std/concepts/object/movable.compile.pass.cpp
libcxx/test/support/type_classification/movable.h
libcxx/test/support/type_classification/moveconstructible.h
libcxx/test/support/type_classification/swappable.h
Modified:
libcxx/include/concepts
libcxx/test/std/concepts/lang/copyconstructible.compile.pass.cpp
libcxx/test/std/concepts/lang/moveconstructible.compile.pass.cpp
libcxx/test/std/concepts/lang/swappable.pass.cpp
libcxx/test/std/concepts/lang/swappable_with.compile.pass.cpp
Removed:
libcxx/test/std/concepts/lang/moveconstructible.h
libcxx/test/std/concepts/lang/swappable.h
################################################################################
diff --git a/libcxx/include/concepts b/libcxx/include/concepts
index 4f97387135fd..696f3536d225 100644
--- a/libcxx/include/concepts
+++ b/libcxx/include/concepts
@@ -366,6 +366,14 @@ concept equality_comparable_with =
const remove_reference_t<_Up>&>> &&
__weakly_equality_comparable_with<_Tp, _Up>;
+// [concepts.object]
+template<class _Tp>
+concept movable =
+ is_object_v<_Tp> &&
+ move_constructible<_Tp> &&
+ assignable_from<_Tp&, _Tp> &&
+ swappable<_Tp>;
+
// [concept.invocable]
template<class _Fn, class... _Args>
concept invocable = requires(_Fn&& __fn, _Args&&... __args) {
diff --git a/libcxx/test/std/concepts/lang/copyconstructible.compile.pass.cpp b/libcxx/test/std/concepts/lang/copyconstructible.compile.pass.cpp
index 327c3c6c399a..95fcb7e62017 100644
--- a/libcxx/test/std/concepts/lang/copyconstructible.compile.pass.cpp
+++ b/libcxx/test/std/concepts/lang/copyconstructible.compile.pass.cpp
@@ -15,7 +15,7 @@
#include <concepts>
#include <type_traits>
-#include "moveconstructible.h"
+#include "type_classification/moveconstructible.h"
// Tests in this namespace are shared with moveconstructible.pass.cpp
// There are some interesting
diff erences, so it's best if they're tested here
diff --git a/libcxx/test/std/concepts/lang/moveconstructible.compile.pass.cpp b/libcxx/test/std/concepts/lang/moveconstructible.compile.pass.cpp
index cdde455c3508..7a1b81588b85 100644
--- a/libcxx/test/std/concepts/lang/moveconstructible.compile.pass.cpp
+++ b/libcxx/test/std/concepts/lang/moveconstructible.compile.pass.cpp
@@ -15,7 +15,7 @@
#include <concepts>
#include <type_traits>
-#include "moveconstructible.h"
+#include "type_classification/moveconstructible.h"
static_assert(std::move_constructible<int>);
static_assert(std::move_constructible<int*>);
@@ -74,4 +74,8 @@ static_assert(std::move_constructible<ImplicitlyDeletedMoveAssign&&>);
static_assert(std::move_constructible<const ImplicitlyDeletedMoveAssign&>);
static_assert(std::move_constructible<const ImplicitlyDeletedMoveAssign&&>);
+static_assert(!std::move_constructible<NonMovable>);
+static_assert(!std::move_constructible<DerivedFromNonMovable>);
+static_assert(!std::move_constructible<HasANonMovable>);
+
int main(int, char**) { return 0; }
diff --git a/libcxx/test/std/concepts/lang/swappable.pass.cpp b/libcxx/test/std/concepts/lang/swappable.pass.cpp
index c16d09969e63..657577676849 100644
--- a/libcxx/test/std/concepts/lang/swappable.pass.cpp
+++ b/libcxx/test/std/concepts/lang/swappable.pass.cpp
@@ -25,8 +25,8 @@
#include <vector>
#include "test_macros.h"
-#include "moveconstructible.h"
-#include "swappable.h"
+#include "type_classification/moveconstructible.h"
+#include "type_classification/swappable.h"
template <class T>
struct expected {
diff --git a/libcxx/test/std/concepts/lang/swappable_with.compile.pass.cpp b/libcxx/test/std/concepts/lang/swappable_with.compile.pass.cpp
index 580762ae530d..46e526dcd0c8 100644
--- a/libcxx/test/std/concepts/lang/swappable_with.compile.pass.cpp
+++ b/libcxx/test/std/concepts/lang/swappable_with.compile.pass.cpp
@@ -26,8 +26,8 @@
#include <unordered_set>
#include <vector>
-#include "moveconstructible.h"
-#include "swappable.h"
+#include "type_classification/moveconstructible.h"
+#include "type_classification/swappable.h"
template <class T, class U>
constexpr bool check_swappable_with_impl() {
diff --git a/libcxx/test/std/concepts/object/movable.compile.pass.cpp b/libcxx/test/std/concepts/object/movable.compile.pass.cpp
new file mode 100644
index 000000000000..5459dcd1940d
--- /dev/null
+++ b/libcxx/test/std/concepts/object/movable.compile.pass.cpp
@@ -0,0 +1,134 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+// UNSUPPORTED: libcpp-no-concepts
+
+// template<class T>
+// concept movable = see below;
+
+#include <concepts>
+
+#include <deque>
+#include <forward_list>
+#include <list>
+#include <map>
+#include <mutex>
+#include <optional>
+#include <unordered_map>
+#include <vector>
+
+#include "type_classification/moveconstructible.h"
+#include "type_classification/movable.h"
+
+// Movable types
+static_assert(std::movable<int>);
+static_assert(std::movable<int volatile>);
+static_assert(std::movable<int*>);
+static_assert(std::movable<int const*>);
+static_assert(std::movable<int volatile*>);
+static_assert(std::movable<int const volatile*>);
+static_assert(std::movable<int (*)()>);
+
+struct S {};
+static_assert(std::movable<S>);
+static_assert(std::movable<int S::*>);
+static_assert(std::movable<int (S::*)()>);
+static_assert(std::movable<int (S::*)() noexcept>);
+static_assert(std::movable<int (S::*)() &>);
+static_assert(std::movable<int (S::*)() & noexcept>);
+static_assert(std::movable<int (S::*)() &&>);
+static_assert(std::movable<int (S::*)() && noexcept>);
+static_assert(std::movable<int (S::*)() const>);
+static_assert(std::movable<int (S::*)() const noexcept>);
+static_assert(std::movable<int (S::*)() const&>);
+static_assert(std::movable<int (S::*)() const & noexcept>);
+static_assert(std::movable<int (S::*)() const&&>);
+static_assert(std::movable<int (S::*)() const && noexcept>);
+static_assert(std::movable<int (S::*)() volatile>);
+static_assert(std::movable<int (S::*)() volatile noexcept>);
+static_assert(std::movable<int (S::*)() volatile&>);
+static_assert(std::movable<int (S::*)() volatile & noexcept>);
+static_assert(std::movable<int (S::*)() volatile&&>);
+static_assert(std::movable<int (S::*)() volatile && noexcept>);
+static_assert(std::movable<int (S::*)() const volatile>);
+static_assert(std::movable<int (S::*)() const volatile noexcept>);
+static_assert(std::movable<int (S::*)() const volatile&>);
+static_assert(std::movable<int (S::*)() const volatile & noexcept>);
+static_assert(std::movable<int (S::*)() const volatile&&>);
+static_assert(std::movable<int (S::*)() const volatile && noexcept>);
+
+static_assert(std::movable<std::deque<int> >);
+static_assert(std::movable<std::forward_list<int> >);
+static_assert(std::movable<std::list<int> >);
+static_assert(std::movable<std::optional<std::vector<int> > >);
+static_assert(std::movable<std::vector<int> >);
+
+static_assert(std::movable<traditional_copy_assignment_only>);
+static_assert(std::movable<has_volatile_member>);
+static_assert(std::movable<has_array_member>);
+
+// Not objects
+static_assert(!std::movable<int&>);
+static_assert(!std::movable<int const&>);
+static_assert(!std::movable<int volatile&>);
+static_assert(!std::movable<int const volatile&>);
+static_assert(!std::movable<int&&>);
+static_assert(!std::movable<int const&&>);
+static_assert(!std::movable<int volatile&&>);
+static_assert(!std::movable<int const volatile&&>);
+static_assert(!std::movable<int()>);
+static_assert(!std::movable<int (&)()>);
+static_assert(!std::movable<int[5]>);
+
+// Core non-move assignable.
+static_assert(!std::movable<int const>);
+static_assert(!std::movable<int const volatile>);
+
+static_assert(!std::movable<DeletedMoveCtor>);
+static_assert(!std::movable<ImplicitlyDeletedMoveCtor>);
+static_assert(!std::movable<DeletedMoveAssign>);
+static_assert(!std::movable<ImplicitlyDeletedMoveAssign>);
+static_assert(!std::movable<NonMovable>);
+static_assert(!std::movable<DerivedFromNonMovable>);
+static_assert(!std::movable<HasANonMovable>);
+
+static_assert(std::movable<cpp03_friendly>);
+static_assert(std::movable<const_move_ctor>);
+static_assert(std::movable<volatile_move_ctor>);
+static_assert(std::movable<cv_move_ctor>);
+static_assert(std::movable<multi_param_move_ctor>);
+static_assert(!std::movable<not_quite_multi_param_move_ctor>);
+
+static_assert(!std::assignable_from<copy_assign_with_mutable_parameter&,
+ copy_assign_with_mutable_parameter>);
+static_assert(!std::movable<copy_assign_with_mutable_parameter>);
+
+static_assert(!std::movable<const_move_assignment>);
+static_assert(std::movable<volatile_move_assignment>);
+static_assert(!std::movable<cv_move_assignment>);
+
+static_assert(!std::movable<const_move_assign_and_traditional_move_assign>);
+static_assert(!std::movable<volatile_move_assign_and_traditional_move_assign>);
+static_assert(!std::movable<cv_move_assign_and_traditional_move_assign>);
+static_assert(std::movable<const_move_assign_and_default_ops>);
+static_assert(std::movable<volatile_move_assign_and_default_ops>);
+static_assert(std::movable<cv_move_assign_and_default_ops>);
+
+static_assert(!std::movable<has_const_member>);
+static_assert(!std::movable<has_cv_member>);
+static_assert(!std::movable<has_lvalue_reference_member>);
+static_assert(!std::movable<has_rvalue_reference_member>);
+static_assert(!std::movable<has_function_ref_member>);
+
+static_assert(std::movable<deleted_assignment_from_const_rvalue>);
+
+// `move_constructible and assignable_from<T&, T>` implies `swappable<T>`,
+// so there's nothing to test for the case of non-swappable.
+
+int main(int, char**) { return 0; }
diff --git a/libcxx/test/support/type_classification/movable.h b/libcxx/test/support/type_classification/movable.h
new file mode 100644
index 000000000000..842bd248cf24
--- /dev/null
+++ b/libcxx/test/support/type_classification/movable.h
@@ -0,0 +1,162 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+#ifndef TEST_SUPPORT_TYPE_CLASSIFICATION_MOVABLE_H
+#define TEST_SUPPORT_TYPE_CLASSIFICATION_MOVABLE_H
+
+struct has_const_member {
+ int const x;
+};
+
+struct has_volatile_member {
+ int volatile x;
+};
+
+struct has_cv_member {
+ int const volatile x;
+};
+
+struct has_lvalue_reference_member {
+ int& x;
+};
+
+struct has_rvalue_reference_member {
+ int&& x;
+};
+
+struct has_array_member {
+ int x[5];
+};
+
+struct has_function_ref_member {
+ int (&f)();
+};
+
+struct cpp03_friendly {
+ cpp03_friendly(cpp03_friendly const&);
+ cpp03_friendly& operator=(cpp03_friendly const&);
+};
+
+struct const_move_ctor {
+ const_move_ctor(const_move_ctor const&&);
+ const_move_ctor& operator=(const_move_ctor&&);
+};
+
+struct volatile_move_ctor {
+ volatile_move_ctor(volatile_move_ctor volatile&&);
+ volatile_move_ctor& operator=(volatile_move_ctor&&);
+};
+
+struct cv_move_ctor {
+ cv_move_ctor(cv_move_ctor const volatile&&);
+ cv_move_ctor& operator=(cv_move_ctor&&);
+};
+
+struct multi_param_move_ctor {
+ multi_param_move_ctor(multi_param_move_ctor&&, int = 0);
+ multi_param_move_ctor& operator=(multi_param_move_ctor&&);
+};
+
+struct not_quite_multi_param_move_ctor {
+ not_quite_multi_param_move_ctor(not_quite_multi_param_move_ctor&&, int);
+ not_quite_multi_param_move_ctor& operator=(not_quite_multi_param_move_ctor&&);
+};
+
+struct traditional_copy_assignment_only {
+ traditional_copy_assignment_only&
+ operator=(traditional_copy_assignment_only const&);
+};
+
+struct copy_assign_with_mutable_parameter {
+ copy_assign_with_mutable_parameter&
+ operator=(copy_assign_with_mutable_parameter&);
+};
+
+struct const_move_assignment {
+ const_move_assignment& operator=(const_move_assignment&&) const;
+};
+
+struct volatile_move_assignment {
+ const_move_assignment& operator=(const_move_assignment&&) volatile;
+};
+
+struct cv_move_assignment {
+ cv_move_assignment& operator=(cv_move_assignment&&) const volatile;
+};
+
+struct const_move_assign_and_traditional_move_assign {
+ const_move_assign_and_traditional_move_assign&
+ operator=(const_move_assign_and_traditional_move_assign&&);
+ const_move_assign_and_traditional_move_assign&
+ operator=(const_move_assign_and_traditional_move_assign&&) const;
+};
+
+struct volatile_move_assign_and_traditional_move_assign {
+ volatile_move_assign_and_traditional_move_assign&
+ operator=(volatile_move_assign_and_traditional_move_assign&&);
+ volatile_move_assign_and_traditional_move_assign&
+ operator=(volatile_move_assign_and_traditional_move_assign&&) volatile;
+};
+
+struct cv_move_assign_and_traditional_move_assign {
+ cv_move_assign_and_traditional_move_assign&
+ operator=(cv_move_assign_and_traditional_move_assign&&);
+ cv_move_assign_and_traditional_move_assign&
+ operator=(cv_move_assign_and_traditional_move_assign&&) const volatile;
+};
+
+struct const_move_assign_and_default_ops {
+ const_move_assign_and_default_ops(const_move_assign_and_default_ops const&) =
+ default;
+ const_move_assign_and_default_ops(const_move_assign_and_default_ops&&) =
+ default;
+ const_move_assign_and_default_ops&
+ operator=(const_move_assign_and_default_ops const&) = default;
+ const_move_assign_and_default_ops&
+ operator=(const_move_assign_and_default_ops&&) = default;
+ const_move_assign_and_default_ops&
+ operator=(const_move_assign_and_default_ops&&) const;
+};
+
+struct volatile_move_assign_and_default_ops {
+ volatile_move_assign_and_default_ops(
+ volatile_move_assign_and_default_ops const&) = default;
+ volatile_move_assign_and_default_ops(volatile_move_assign_and_default_ops&&) =
+ default;
+ volatile_move_assign_and_default_ops&
+ operator=(volatile_move_assign_and_default_ops const&) = default;
+ volatile_move_assign_and_default_ops&
+ operator=(volatile_move_assign_and_default_ops&&) = default;
+ volatile_move_assign_and_default_ops&
+ operator=(volatile_move_assign_and_default_ops&&) volatile;
+};
+
+struct cv_move_assign_and_default_ops {
+ cv_move_assign_and_default_ops(cv_move_assign_and_default_ops const&) =
+ default;
+ cv_move_assign_and_default_ops(cv_move_assign_and_default_ops&&) = default;
+ cv_move_assign_and_default_ops&
+ operator=(cv_move_assign_and_default_ops const&) = default;
+ cv_move_assign_and_default_ops&
+ operator=(cv_move_assign_and_default_ops&&) = default;
+ cv_move_assign_and_default_ops&
+ operator=(cv_move_assign_and_default_ops&&) const volatile;
+};
+
+struct deleted_assignment_from_const_rvalue {
+ deleted_assignment_from_const_rvalue(
+ deleted_assignment_from_const_rvalue const&);
+ deleted_assignment_from_const_rvalue(deleted_assignment_from_const_rvalue&&);
+ deleted_assignment_from_const_rvalue&
+ operator=(const deleted_assignment_from_const_rvalue&);
+ deleted_assignment_from_const_rvalue&
+ operator=(deleted_assignment_from_const_rvalue&&);
+ deleted_assignment_from_const_rvalue&
+ operator=(const deleted_assignment_from_const_rvalue&&) = delete;
+};
+
+#endif // TEST_SUPPORT_TYPE_CLASSIFICATION_MOVABLE_H
diff --git a/libcxx/test/std/concepts/lang/moveconstructible.h b/libcxx/test/support/type_classification/moveconstructible.h
similarity index 89%
rename from libcxx/test/std/concepts/lang/moveconstructible.h
rename to libcxx/test/support/type_classification/moveconstructible.h
index ed585fd00163..780fd6b476c9 100644
--- a/libcxx/test/std/concepts/lang/moveconstructible.h
+++ b/libcxx/test/support/type_classification/moveconstructible.h
@@ -5,8 +5,8 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
-#ifndef TEST_STD_CONCEPTS_LAND_MOVECONSTRUCTIBLE_H
-#define TEST_STD_CONCEPTS_LAND_MOVECONSTRUCTIBLE_H
+#ifndef TEST_SUPPORT_TYPE_CLASSIFICATION_MOVECONSTRUCTIBLE_H
+#define TEST_SUPPORT_TYPE_CLASSIFICATION_MOVECONSTRUCTIBLE_H
struct HasDefaultOps {};
@@ -71,4 +71,4 @@ struct HasANonMovable {
NonMovable X;
};
-#endif // TEST_STD_CONCEPTS_LAND_MOVECONSTRUCTIBLE_H
+#endif // TEST_SUPPORT_TYPE_CLASSIFICATION_MOVECONSTRUCTIBLE_H
diff --git a/libcxx/test/std/concepts/lang/swappable.h b/libcxx/test/support/type_classification/swappable.h
similarity index 98%
rename from libcxx/test/std/concepts/lang/swappable.h
rename to libcxx/test/support/type_classification/swappable.h
index fcede439d1c2..f7916e4b0646 100644
--- a/libcxx/test/std/concepts/lang/swappable.h
+++ b/libcxx/test/support/type_classification/swappable.h
@@ -5,8 +5,8 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
-#ifndef TEST_STD_CONCEPTS_LANG_SWAPPABLE_H
-#define TEST_STD_CONCEPTS_LANG_SWAPPABLE_H
+#ifndef TEST_SUPPORT_TYPE_CLASSIFICATION_SWAPPABLE_H
+#define TEST_SUPPORT_TYPE_CLASSIFICATION_SWAPPABLE_H
#include <concepts>
@@ -263,4 +263,4 @@ class throwable_adl_swappable {
throwable_adl_swappable* this_ = this;
};
-#endif // TEST_STD_CONCEPTS_LANG_SWAPPABLE_H
+#endif // TEST_SUPPORT_TYPE_CLASSIFICATION_SWAPPABLE_H
More information about the libcxx-commits
mailing list