Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <memory>
- #include <string>
- #include <utility>
- struct Container {
- std::string str;
- Container(std::string&& s) : str{ std::move(s) } {
- std::cout << "Constructor!\n";
- }
- ~Container() {
- // std::cout << "Destructor!\n";
- }
- Container(const Container& oth) : str{ oth.str } {
- std::cout << "Copy constructor!\n";
- }
- Container(Container&& oth) noexcept : str{ std::move(oth.str) } {
- std::cout << "Move constructor!\n";
- }
- Container& operator=(const Container& oth) {
- if (this != &oth) {
- str = oth.str;
- }
- std::cout << "Copy assignment!\n";
- return *this;
- }
- Container& operator=(Container&& oth) noexcept {
- if (this != &oth) {
- str = std::move(oth.str);
- }
- std::cout << "Move assignment!\n";
- return *this;
- }
- };
- void section(std::string&& s) {
- std::cout << "\n" << std::string(s.length(), '~') << "\n" << s << "\n";
- }
- int main()
- {
- std::cout << "Values:\n";
- Container a{ "test" };
- Container b = a;
- Container c{ a };
- Container d{ Container{ "test" } };
- Container e = Container{ "test" };
- std::cout << '\n';
- a = b;
- b = Container{ "test" };
- section("Lvalue references:");
- Container& f = a;
- Container& g = f;
- std::cout << '\n';
- f = a;
- g = f;
- section("Rvalue references:");
- Container&& h = Container{ "test" };
- Container&& i = std::move(c);
- std::cout << '\n';
- h = Container{ "test" };
- i = std::move(h);
- section("Values:");
- Container j{ f };
- Container k = g;
- Container l{ h };
- Container m = h;
- std::cout << '\n';
- Container n{ std::move(h) };
- Container o = std::move(h);
- Container p{ std::move(d) };
- Container q = std::move(e);
- Container r{ std::move(f) };
- Container s = std::move(g);
- std::cout << '\n';
- n = f;
- o = h;
- p = std::move(f);
- q = std::move(h);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement