In the following code:
struct copy_only
{
copy_only() = default;
copy_only(const copy_only&) = default;
copy_only& operator=(const copy_only&) = default;
copy_only(copy_only&&) = delete;
copy_only& operator=(copy_only&&) = delete;
~copy_only() = default;
};
std::vector<copy_only> v;
copy_only c{};
v.push_back(c);
On MSVC we get the error:
error C2280: 'copy_only::copy_only(copy_only &&)': attempting to reference a deleted function
This comes from within the vector implementation where push_back(const&)
calls emplace_back
implementation:
note: while compiling class template member function 'void std::vector<copy_only,std::allocator<copy_only>>::push_back(const _Ty &)'
note: see reference to function template instantiation '_Ty &std::vector<_Ty,std::allocator<_Ty>>::_Emplace_one_at_back<const _Ty&>(const _Ty &)' being compiled
This compiles with gcc and clang. Is this just a massive MSVC compiler bug on the simplest vector example? Or is there some standard thing that would prevent this usage that gcc and clang are just glossing over?
T
is not CopyInsertable into vector. 2)T
is not MoveInsertable into vector." Similarly foremplace_back
. All compilers are correct, since the code exhibits undefined behavior.libc++
as well, which prints clear errorThe specified type does not meet the requirements of Cpp17MoveInsertable
: gcc.godbolt.org/z/8T6fbWe4e