[libcxx] adds concept std::movable
authorChristopher Di Bella <cjdb.ns@gmail.com>
Fri, 12 Mar 2021 02:54:57 +0000 (18:54 -0800)
committerChristopher Di Bella <cjdb.ns@gmail.com>
Fri, 12 Mar 2021 03:27:43 +0000 (19:27 -0800)
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

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
libcxx/test/std/concepts/object/movable.compile.pass.cpp [new file with mode: 0644]
libcxx/test/support/type_classification/movable.h [new file with mode: 0644]
libcxx/test/support/type_classification/moveconstructible.h [moved from libcxx/test/std/concepts/lang/moveconstructible.h with 89% similarity]
libcxx/test/support/type_classification/swappable.h [moved from libcxx/test/std/concepts/lang/swappable.h with 98% similarity]

index 4f97387..696f353 100644 (file)
@@ -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) {
index 327c3c6..95fcb7e 100644 (file)
@@ -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 differences, so it's best if they're tested here
index cdde455..7a1b815 100644 (file)
@@ -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; }
index c16d099..6575776 100644 (file)
@@ -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 {
index 580762a..46e526d 100644 (file)
@@ -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 (file)
index 0000000..5459dcd
--- /dev/null
@@ -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 (file)
index 0000000..842bd24
--- /dev/null
@@ -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
@@ -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
@@ -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 @@ private:
   throwable_adl_swappable* this_ = this;
 };
 
-#endif // TEST_STD_CONCEPTS_LANG_SWAPPABLE_H
+#endif // TEST_SUPPORT_TYPE_CLASSIFICATION_SWAPPABLE_H