CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List
C++ Code Walkthrough #65

std::move on a const Object Still Copies

hardClasses

📄 Code

Read carefully — what does this print?
1#include <iostream>
2#include <utility>
3using namespace std;
4 
5class Tag {
6public:
7 Tag() { cout << "default" << endl; }
8 Tag(const Tag& other) { cout << "copy" << endl; }
9 Tag(Tag&& other) noexcept { cout << "move" << endl; }
10};
11 
12int main() {
13 const Tag a;
14 Tag b = std::move(a);
15 Tag c;
16 Tag d = std::move(c);
17 const Tag& ref = d;
18 Tag e = std::move(ref);
19 return 0;
20}

🎯 Your Answer