A C++ Image class gained a user-declared destructor and now std::move(image) copies its pixels; which special members does the compiler still declare, and why?
answer
- user-declared, not user-provided
- a destructor blocks implicit moves
- copies still generated, deprecated
- rvalues bind to const Image&
- delete it or declare five
basics
~10 sA user-declared destructor, even = default, stops the compiler declaring the move constructor and move assignment, while the copy operations are still generated, a deprecated case. std::move(image) then binds to const Image& and copies.
solid answer
~50 sThe standard declares an implicit move constructor only if the class has no user-declared copy constructor, copy assignment, move assignment or destructor; move assignment has the matching rule. Adding `~Image() {}` for logging, or `~Image() = default;` for clarity, is a user-declared destructor, so both moves disappear. The copy constructor and copy assignment are still implicitly defined as defaulted, a case the standard deprecates. `std::move(image)` is only a cast to an rvalue, and with no move constructor the rvalue binds to `const Image&`, so the pixels are copied. Nothing fails: `std::is_move_constructible_v<Image>` is still true, because it asks whether construction from an rvalue compiles, not which constructor runs. `std::vector<Image>` reallocation copies as well. The fixes are to remove the destructor if the members already clean up, or to declare all five, using `= default` for the moves.
code
cpp · 23 lines#include <cstdio>
#include <type_traits>
#include <utility>
struct Pixels { // stands in for an expensive-to-copy buffer
Pixels() = default;
Pixels(const Pixels&) { std::puts(" Pixels copied"); }
Pixels(Pixels&&) noexcept { std::puts(" Pixels moved"); }
};
class Image {
public:
~Image() {} // user-declared destructor: no implicit move
private:
Pixels pixels_;
};
int main() {
static_assert(std::is_move_constructible_v<Image>); // true: the copy ctor binds rvalues
Image a;
std::puts("Image b = std::move(a):");
Image b = std::move(a); // selects the implicit copy constructor
}go deeper
Recall that the compiler can write six special members for you, and that declaring some of them changes which others it writes.
State that a user-declared destructor or copy operation stops the implicit moves, while copies remain, deprecated, and that std::move is only a cast.
Diagnose the silent copy from the generation rules, explain why the traits and default warnings miss it, and guard the class with a nothrow-move static_assert.
Turn the table into a review rule, declare none or declare all five, and choose warnings and static assertions that keep accidental copies out of hot paths.
## The generation rules The compiler can declare six special members: default constructor, destructor, copy constructor, copy assignment, move constructor and move assignment. Whether it does depends on what the class **user-declares**, meaning declares at all, including with `= default` or `= delete`. The C++ standard's rules, condensed: | User declares | Default ctor | Destructor | Copy ctor | Copy assign | Move ctor | Move assign | |---|---|---|---|---|---|---| | nothing | defaulted | defaulted | defaulted | defaulted | defaulted | defaulted | | any constructor | not declared | defaulted | defaulted | defaulted | defaulted | defaulted | | destructor | defaulted | user | defaulted, deprecated | defaulted, deprecated | not declared | not declared | | copy constructor | not declared | defaulted | user | defaulted, deprecated | not declared | not declared | | copy assignment | defaulted | defaulted | defaulted, deprecated | user | not declared | not declared | | move constructor | not declared | defaulted | deleted | deleted | user | not declared | | move assignment | defaulted | defaulted | deleted | deleted | not declared | user | Three patterns carry most interview questions: - **Moves are fragile.** Any user-declared copy operation, the other move operation or a destructor stops them being declared. - **Copies are sticky.** They are still generated next to a user-declared destructor or the other copy operation, but the standard deprecates this and notes that future versions may define them as deleted. - **Declaring a move deletes the copies.** The implicitly declared copy operations are defined as deleted, so a class with only a hand-written move constructor is move-only. "Defaulted" here means declared and defined by the compiler; a defaulted member can still end up defined as deleted if a member or base cannot support it. ## The Image regression `Image` started with only members that manage themselves, such as a `std::vector<unsigned char>` of pixels, so it had all six special members implicitly. Someone added `~Image() { log("image freed"); }`, or `~Image() = default;` to make the destructor visible. Either one is user-declared, so: 1. The move constructor and move assignment are **not declared**. 2. The copy constructor and copy assignment are still **implicitly defaulted**. 3. `Image b = std::move(a);` casts `a` to an rvalue. With no move constructor, overload resolution picks `Image(const Image&)`, whose parameter binds to rvalues, and copies every pixel. The code example shows this with a `Pixels` member that prints whether it was copied or moved: after adding the destructor, `std::move` prints `Pixels copied`. The same happens with `~Image() = default;`. ## Why nothing complains - The program is correct, only slower, so no error is possible. - `static_assert(std::is_move_constructible_v<Image>)` still passes, because the trait asks whether `Image(std::move(a))` is well-formed, and the copy constructor makes it so. - `-Wall` and `-Wextra` are silent. Clang's `-Wdeprecated-copy-with-dtor` reports the deprecated implicit copy constructor only when a copy is actually made, and only if the flag is enabled; GCC has `-Wdeprecated-copy-dtor`, which `-Wextra` does not enable either. - `std::vector<Image>` reallocations copy instead of moving, because there is no move constructor to use. ## Fixing it Pick one of two shapes: 1. **Remove the destructor** if the members already release everything. This is the rule of zero, and it restores all six implicit members. 2. **Declare all five** when a destructor is genuinely needed, as for a polymorphic base with `virtual ~Image() = default;`: add `Image(const Image&) = default;`, `Image& operator=(const Image&) = default;`, `Image(Image&&) noexcept = default;` and `Image& operator=(Image&&) noexcept = default;`. Declaring the copies explicitly also removes the deprecated case. To make the regression impossible to reintroduce silently, assert the property you need, for example `static_assert(std::is_nothrow_move_constructible_v<Image>);`. With the destructor added and the moves gone, the copy constructor that would be chosen is not `noexcept`, so the assertion fails. ## Where the regression usually comes from The destructor rarely arrives for a good reason. Common sources are a debug log line added while chasing a leak, `~Image() = default;` added by a style rule that wants every class to show its destructor, a `virtual` destructor added when the class became a base, and a destructor added to free one member that should have been a smart pointer. Each is a one-line change that passes every functional test, which is why the check belongs in the type, not in review memory. ## How to answer Quote the condition for the implicit move constructor, say that `= default` still counts as user-declared, explain that `std::move` is only a cast so the copy constructor is chosen, and give both fixes plus a check that would have caught it.
- In C++, does ~Image() = default; keep the implicit move operations?No. Explicitly defaulting the destructor on its first declaration makes it not user-provided, but it is still user-declared, and the rule for implicit moves looks at user-declared destructors. So the moves are not declared and rvalues copy. Either omit the destructor or default the moves explicitly as well.
- Why does std::is_nothrow_move_constructible_v<Image> catch this regression when std::is_move_constructible_v does not?`is_move_constructible` only asks whether constructing from an rvalue compiles, and the copy constructor makes it compile. The nothrow variant also requires the selected constructor to be non-throwing. A compiler-generated copy constructor that copies a `std::vector` member is not `noexcept`, so once the moves disappear the nothrow assertion fails at compile time.
- Declaring a destructor = default keeps the compiler-generated move operations.
- std::move forces a move even when the class has no move constructor.
- std::is_move_constructible_v being true proves the class has a move constructor.
- A user-declared destructor deletes the copy constructor.
- Declaring any constructor removes the implicit copy and move operations.