Skip to content

Instantly share code, notes, and snippets.

@jmlyn
Last active April 20, 2020 03:54
Show Gist options
  • Select an option

  • Save jmlyn/7a7928d65ecefcf623a2fd586a0539b2 to your computer and use it in GitHub Desktop.

Select an option

Save jmlyn/7a7928d65ecefcf623a2fd586a0539b2 to your computer and use it in GitHub Desktop.
#include <boost/type_index.hpp>
#include <iostream>
#include <utility>
using namespace std;
using boost::typeindex::type_id_with_cvr;
class Widget {
public:
Widget() = default;
Widget(Widget&& rhs) noexcept : data(move(rhs.data)) {
cout << "Move ctor" << endl;
}
// Compiler implicitly does copy ctor when moving if you define a copy ctor without a move ctor
Widget(const Widget& rhs) {
cout << "Copy ctor" << endl;
data = rhs.data;
}
int data = 0;
};
int main() {
auto w1 = Widget();
w1.data = 10;
cout << w1.data << endl;
Widget w2(w1);
cout << w2.data << endl;
auto w3 = move(w1);
w3.data = 15;
cout << w3.data << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment