From 37f46650c3866c06c6108740fd15a34ebcb6a404 Mon Sep 17 00:00:00 2001 From: Louis Dionne <ldionne@apple.com> Date: Tue, 11 Feb 2020 17:11:00 +0100 Subject: [PATCH] [libc++] Make sure that vector copy-construction is disabled for non-copyable types The Standard requires the value_type of the vector to be Cpp17CopyInsertable in order for copy-construction to be enabled: http://eel.is/c++draft/container.requirements#tab:container.req rdar://problem/56674564 Differential Revision: https://reviews.llvm.org/D74251 --- libcxx/include/memory | 2 +- .../vector.cons/copy.move_only.fail.cpp | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 libcxx/test/std/containers/sequences/vector/vector.cons/copy.move_only.fail.cpp diff --git a/libcxx/include/memory b/libcxx/include/memory index 34c3e0c0d8d1..821f371eb0b0 100644 --- a/libcxx/include/memory +++ b/libcxx/include/memory @@ -1695,7 +1695,7 @@ struct _LIBCPP_TEMPLATE_VIS allocator_traits static typename enable_if < - is_trivially_move_constructible<_DestTp>::value && + is_trivially_copy_constructible<_DestTp>::value && is_same<_RawSourceTp, _RawDestTp>::value && (__is_default_allocator<allocator_type>::value || !__has_construct<allocator_type, _DestTp*, _SourceTp&>::value), diff --git a/libcxx/test/std/containers/sequences/vector/vector.cons/copy.move_only.fail.cpp b/libcxx/test/std/containers/sequences/vector/vector.cons/copy.move_only.fail.cpp new file mode 100644 index 000000000000..b38f24c3be25 --- /dev/null +++ b/libcxx/test/std/containers/sequences/vector/vector.cons/copy.move_only.fail.cpp @@ -0,0 +1,28 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// Make sure that a std::vector containing move-only types can't be copied. + +// UNSUPPORTED: c++98, c++03 +// REQUIRES: verify-support + +#include <vector> + +struct move_only +{ + move_only() = default; + move_only(move_only&&) = default; + move_only& operator=(move_only&&) = default; +}; + +int main(int, char**) +{ + std::vector<move_only> v; + std::vector<move_only> copy = v; // expected-error@memory:* {{call to implicitly-deleted copy constructor of 'move_only'}} + return 0; +}