[libcxx-commits] [libcxx] 8ef69c6 - [libcxx] adds concept std::copyable

Christopher Di Bella via libcxx-commits libcxx-commits at lists.llvm.org
Thu Mar 11 19:47:39 PST 2021


Author: Christopher Di Bella
Date: 2021-03-11T19:30:55-08:00
New Revision: 8ef69c66d5aab9ca2f04e0aecf109a2d2f7bb4b9

URL: https://github.com/llvm/llvm-project/commit/8ef69c66d5aab9ca2f04e0aecf109a2d2f7bb4b9
DIFF: https://github.com/llvm/llvm-project/commit/8ef69c66d5aab9ca2f04e0aecf109a2d2f7bb4b9.diff

LOG: [libcxx] adds concept std::copyable

Implements parts of:
    - P0898R3 Standard Library Concepts
    - P1754 Rename concepts to standard_case for C++20, while we still can

Depends on D97359

Reviewed By: EricWF, #libc, Quuxplusone, zoecarver

Differential Revision: https://reviews.llvm.org/D97443

Added: 
    libcxx/test/std/concepts/object/copyable.compile.pass.cpp
    libcxx/test/support/type_classification/copyable.h

Modified: 
    libcxx/include/concepts

Removed: 
    


################################################################################
diff  --git a/libcxx/include/concepts b/libcxx/include/concepts
index 696f3536d225..cb571b424553 100644
--- a/libcxx/include/concepts
+++ b/libcxx/include/concepts
@@ -374,6 +374,14 @@ concept movable =
   assignable_from<_Tp&, _Tp> &&
   swappable<_Tp>;
 
+template<class _Tp>
+concept copyable =
+  copy_constructible<_Tp> &&
+  movable<_Tp> &&
+  assignable_from<_Tp&, _Tp&> &&
+  assignable_from<_Tp&, const _Tp&> &&
+  assignable_from<_Tp&, const _Tp>;
+
 // [concept.invocable]
 template<class _Fn, class... _Args>
 concept invocable = requires(_Fn&& __fn, _Args&&... __args) {

diff  --git a/libcxx/test/std/concepts/object/copyable.compile.pass.cpp b/libcxx/test/std/concepts/object/copyable.compile.pass.cpp
new file mode 100644
index 000000000000..605308fbe3c7
--- /dev/null
+++ b/libcxx/test/std/concepts/object/copyable.compile.pass.cpp
@@ -0,0 +1,118 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 From>
+// concept copyable = see below;
+
+#include <concepts>
+
+#include <deque>
+#include <forward_list>
+#include <list>
+#include <map>
+#include <memory>
+#include <optional>
+#include <unordered_map>
+#include <vector>
+
+#include "type_classification/copyable.h"
+
+static_assert(std::copyable<int>);
+static_assert(std::copyable<int volatile>);
+static_assert(std::copyable<int*>);
+static_assert(std::copyable<int const*>);
+static_assert(std::copyable<int volatile*>);
+static_assert(std::copyable<int volatile const*>);
+static_assert(std::copyable<int (*)()>);
+
+struct S {};
+static_assert(std::copyable<S>);
+static_assert(std::copyable<int S::*>);
+static_assert(std::copyable<int (S::*)()>);
+static_assert(std::copyable<int (S::*)() noexcept>);
+static_assert(std::copyable<int (S::*)() &>);
+static_assert(std::copyable<int (S::*)() & noexcept>);
+static_assert(std::copyable<int (S::*)() &&>);
+static_assert(std::copyable<int (S::*)() && noexcept>);
+static_assert(std::copyable<int (S::*)() const>);
+static_assert(std::copyable<int (S::*)() const noexcept>);
+static_assert(std::copyable<int (S::*)() const&>);
+static_assert(std::copyable<int (S::*)() const & noexcept>);
+static_assert(std::copyable<int (S::*)() const&&>);
+static_assert(std::copyable<int (S::*)() const && noexcept>);
+static_assert(std::copyable<int (S::*)() volatile>);
+static_assert(std::copyable<int (S::*)() volatile noexcept>);
+static_assert(std::copyable<int (S::*)() volatile&>);
+static_assert(std::copyable<int (S::*)() volatile & noexcept>);
+static_assert(std::copyable<int (S::*)() volatile&&>);
+static_assert(std::copyable<int (S::*)() volatile && noexcept>);
+static_assert(std::copyable<int (S::*)() const volatile>);
+static_assert(std::copyable<int (S::*)() const volatile noexcept>);
+static_assert(std::copyable<int (S::*)() const volatile&>);
+static_assert(std::copyable<int (S::*)() const volatile & noexcept>);
+static_assert(std::copyable<int (S::*)() const volatile&&>);
+static_assert(std::copyable<int (S::*)() const volatile && noexcept>);
+
+static_assert(std::copyable<std::vector<int> >);
+static_assert(std::copyable<std::deque<int> >);
+static_assert(std::copyable<std::forward_list<int> >);
+static_assert(std::copyable<std::list<int> >);
+static_assert(std::copyable<std::shared_ptr<std::unique_ptr<int> > >);
+static_assert(std::copyable<std::optional<std::vector<int> > >);
+static_assert(std::copyable<std::vector<int> >);
+static_assert(std::copyable<std::vector<std::unique_ptr<int> > >);
+
+static_assert(std::copyable<has_volatile_member>);
+static_assert(std::copyable<has_array_member>);
+
+// Not objects
+static_assert(!std::copyable<void>);
+static_assert(!std::copyable<int&>);
+static_assert(!std::copyable<int const&>);
+static_assert(!std::copyable<int volatile&>);
+static_assert(!std::copyable<int const volatile&>);
+static_assert(!std::copyable<int&&>);
+static_assert(!std::copyable<int const&&>);
+static_assert(!std::copyable<int volatile&&>);
+static_assert(!std::copyable<int const volatile&&>);
+static_assert(!std::copyable<int()>);
+static_assert(!std::copyable<int (&)()>);
+static_assert(!std::copyable<int[5]>);
+
+// Not copy constructible or copy assignable
+static_assert(!std::copyable<std::unique_ptr<int> >);
+
+// Not assignable
+static_assert(!std::copyable<int const>);
+static_assert(!std::copyable<int const volatile>);
+static_assert(std::copyable<const_copy_assignment const>);
+static_assert(!std::copyable<volatile_copy_assignment volatile>);
+static_assert(std::copyable<cv_copy_assignment const volatile>);
+
+static_assert(!std::copyable<no_copy_constructor>);
+static_assert(!std::copyable<no_copy_assignment>);
+
+static_assert(std::is_copy_assignable_v<no_copy_assignment_mutable>);
+static_assert(!std::copyable<no_copy_assignment_mutable>);
+static_assert(!std::copyable<derived_from_noncopyable>);
+static_assert(!std::copyable<has_noncopyable>);
+static_assert(!std::copyable<has_const_member>);
+static_assert(!std::copyable<has_cv_member>);
+static_assert(!std::copyable<has_lvalue_reference_member>);
+static_assert(!std::copyable<has_rvalue_reference_member>);
+static_assert(!std::copyable<has_function_ref_member>);
+
+static_assert(
+    !std::assignable_from<deleted_assignment_from_const_rvalue&,
+                          deleted_assignment_from_const_rvalue const>);
+static_assert(!std::copyable<deleted_assignment_from_const_rvalue>);
+
+int main(int, char**) { return 0; }

diff  --git a/libcxx/test/support/type_classification/copyable.h b/libcxx/test/support/type_classification/copyable.h
new file mode 100644
index 000000000000..cae224fe997b
--- /dev/null
+++ b/libcxx/test/support/type_classification/copyable.h
@@ -0,0 +1,77 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_COPYABLE_H
+#define TEST_SUPPORT_TYPE_CLASSIFICATION_COPYABLE_H
+
+#include <memory>
+
+#include "movable.h"
+
+struct no_copy_constructor {
+  no_copy_constructor() = default;
+
+  no_copy_constructor(no_copy_constructor const&) = delete;
+  no_copy_constructor(no_copy_constructor&&) = default;
+};
+
+struct no_copy_assignment {
+  no_copy_assignment() = default;
+
+  no_copy_assignment& operator=(no_copy_assignment const&) = delete;
+  no_copy_assignment& operator=(no_copy_assignment&&) = default;
+};
+
+struct no_copy_assignment_mutable {
+  no_copy_assignment_mutable() = default;
+
+  no_copy_assignment_mutable&
+  operator=(no_copy_assignment_mutable const&) = default;
+  no_copy_assignment_mutable& operator=(no_copy_assignment_mutable&) = delete;
+  no_copy_assignment_mutable& operator=(no_copy_assignment_mutable&&) = default;
+};
+
+struct derived_from_noncopyable : std::unique_ptr<int> {};
+
+struct has_noncopyable {
+  std::unique_ptr<int> x;
+};
+
+struct const_copy_assignment {
+  const_copy_assignment() = default;
+
+  const_copy_assignment(const_copy_assignment const&);
+  const_copy_assignment(const_copy_assignment&&);
+
+  const_copy_assignment& operator=(const_copy_assignment&&);
+  const_copy_assignment const& operator=(const_copy_assignment const&) const;
+};
+
+struct volatile_copy_assignment {
+  volatile_copy_assignment() = default;
+
+  volatile_copy_assignment(volatile_copy_assignment volatile&);
+  volatile_copy_assignment(volatile_copy_assignment volatile&&);
+
+  volatile_copy_assignment& operator=(volatile_copy_assignment&&);
+  volatile_copy_assignment volatile&
+  operator=(volatile_copy_assignment const&) volatile;
+};
+
+struct cv_copy_assignment {
+  cv_copy_assignment() = default;
+
+  cv_copy_assignment(cv_copy_assignment const volatile&);
+  cv_copy_assignment(cv_copy_assignment const volatile&&);
+
+  cv_copy_assignment const volatile&
+  operator=(cv_copy_assignment const volatile&) const volatile;
+  cv_copy_assignment const volatile&
+  operator=(cv_copy_assignment const volatile&&) const volatile;
+};
+
+#endif // TEST_SUPPORT_TYPE_CLASSIFICATION_COPYABLE_H


        


More information about the libcxx-commits mailing list