In general, the C++ Standard Library types are designed with both copy and move semantics in mind.
Is there any type defined in the Standard Library which has a copy constructor but doesn’t have a move constructor?
4
Yes, there are classes in the standard library with a copy constructor but without a move constructor.
As Aykhan Hagverdili commented above std::allocator
is an example for such a class.
But note that a lack of move constructor does not mean that the object cannot be used as an rvalue.
It simply means that the copy constructor will be used in this case.
In a way you can consider a move constuctor as simply an optimization for that case, which can always fallback to copying.
In order to prohibit using it as an ravalue, the move constructor would need to be explicitly delete
d.
I am not aware of any standard library class that has a copy constructor and a delete
d move constrcutor.
10
Yes, there are such types. An example can be std::span<T>
or std::string_view
classes. They both have a user-declared copy constructor (defined as defaulted), which avoids implicit declaration of a move constructor.
Why don’t these classes have move constructor? Simply because having it wouldn’t make any sense. Note that both are required to be trivially copyable (string_view
since C++23). This implies that even if they had a move constructor, it would be trivial. And trivial move constructors perform bitwise copy, same as trivial copy constructors.
Even if these classes wouldn’t be required to be trivially copyable, providing non-trivial move constructor wouldn’t make any sense. Their objects do not own anything, and requiring them to get into an “empty state” after move would actually make moves more expensive than copies.
2