Created
April 19, 2018 01:28
-
-
Save wilhuff/61890e0bd2ea080f4c5827a9c7ddefd7 to your computer and use it in GitHub Desktop.
Does copy elision make std::move obsolete?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <utility> | |
struct Noisy { | |
Noisy() {} | |
Noisy(const Noisy&) { std::cout << "copied\n"; } | |
Noisy(Noisy&&) {} | |
~Noisy() {} | |
}; | |
struct MoveContainer { | |
MoveContainer(Noisy noisy) : noisy_(std::move(noisy)) { | |
} | |
Noisy noisy_; | |
}; | |
void TestMoveContainer() { | |
Noisy arg; | |
MoveContainer container(arg); | |
} | |
void TestMoveMoveContainer() { | |
Noisy arg; | |
MoveContainer container(std::move(arg)); | |
} | |
struct ByValueContainer { | |
ByValueContainer(Noisy noisy) : noisy_(noisy) { | |
} | |
Noisy noisy_; | |
}; | |
void TestByValueContainer() { | |
Noisy arg; | |
ByValueContainer container(arg); | |
} | |
int main() { | |
std::cout << "by-value container:\n"; | |
TestByValueContainer(); | |
std::cout << "\nmove container:\n"; | |
TestMoveContainer(); | |
std::cout << "\nmove move container:\n"; | |
TestMoveMoveContainer(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment