I have following class
class widget {
// The methods only print their name, i.e. c'tor, destructor etc.
public:
widget();
widget(const widget&);
widget(widget&&);
~widget();
auto operator=(const widget&) -> widget&;
auto operator=(widget&&) -> widget&;
};
which I use in following code
#include "widget.h"
auto main() -> int {
widget c(std::move(widget()));
c = std::move(widget());
return 0;
};
The resulting behavior is comprehensible to me. In the first call a widget is constructed, then the move constructor is invoked and the destructor is called on the temporary widget.
The second call does the same, expect for calling the move assignment operator instead of the move constructor.
Leaving the main method, the destructor is invoked on c
.
Now comes the interesting part:
#include "widget.h"
auto main() -> int {
widget c((widget()));
c = widget();
return 0;
};
If I leave out the call to std::move
, the first case stops working and results in just one constructor call. Whereas the second case is still working as before.
What am I missing here? Why are those two function calls treating their parameters differently? I tried this on gcc and clang.