Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cassert>
- #include <cmath>
- using namespace std;
- class Double;
- Double&& move(Double&& a) { return a; }
- static inline int IDENTIFY(const void* ptr) { return (((int)ptr) >> 2) & 0xFF; }
- class Double {
- public:
- double value;
- Double(const double _value) : value(_value) {
- printf("Constructor, [%x][?] := [?][%lg].\n", IDENTIFY(this), value);
- }
- Double(const Double& other) : value(other.value) {
- printf("Copy constructor, [%x][?] := [%x][%lg]\n", IDENTIFY(this), IDENTIFY(&other), value);
- }
- Double(Double&& other) : value(other.value) {
- printf("Move constructor, [%x][?] <- [%x][%lg]\n", IDENTIFY(this), IDENTIFY(&other), value);
- other.value = nan("move");
- }
- ~Double() {
- value = nan("dstr");
- printf("Destructor, delete [%x][%lg]\n", IDENTIFY(this), value);
- }
- Double& operator= (const Double& other) {
- printf("Copy operator, [%x][%lg] := [%x][%lg]\n", IDENTIFY(this), value, IDENTIFY(&other), other.value);
- value = other.value;
- return *this;
- }
- Double& operator= (Double&& other) {
- printf("Move operator, [%x][%lg] <- [%x][%lg]\n", IDENTIFY(this), value, IDENTIFY(&other), other.value);
- value = other.value;
- return *this;
- }
- Double& operator+= (const Double& other) {
- printf("Increament, [%x][%lg] += [%x][%lg]\n", IDENTIFY(this), value, IDENTIFY(&other), other.value);
- value += other.value;
- return *this;
- }
- bool operator== (const double v) { return v == value; }
- };
- Double operator+ (const Double& self, const Double& other) {
- printf("Addition (copy), return [%x][%lg] + [%x][%lg]\n", IDENTIFY(&self), self.value, IDENTIFY(&other), other.value);
- Double copy = self;
- return (Double&&)(copy += other);
- }
- Double operator+ (Double&& self, const Double& other) {
- printf("Addition (temp), return [%x][%lg] + [%x][%lg]\n", IDENTIFY(&self), self.value, IDENTIFY(&other), other.value);
- return (Double&&)(self += other);
- }
- int main () {
- Double a = 1.;
- Double b = 10.;
- Double c = a;
- Double d = b;
- Double e = c;
- Double z = 0;
- assert(a == 1);
- assert(b == 10);
- assert(c == 1);
- assert(d == 10);
- assert(e == 1);
- assert(z == 0);
- printf("\n\nf = a + z + a + z + c + z + z + z;\n");
- Double f = a + z + a + z + c + z + z + z;
- assert(f == 3);
- assert(a == 1);
- assert(c == 1);
- assert(z == 0);
- printf("\n\nf += a;\n");
- f += a;
- assert(f == 4);
- assert(a == 1);
- printf("\n\ne = b;\n");
- e = b;
- assert(e == 10);
- assert(b == 10);
- printf("\n\na = c + a + f;\n");
- a = c + a + z + z + f;
- assert(c == 1);
- assert(f == 4);
- assert(a == 6);
- assert(z == 0);
- return 0;
- }
- /* Result:
- Constructor, [e2][?] := [?][1].
- Constructor, [e0][?] := [?][10].
- Copy constructor, [de][?] := [e2][1]
- Copy constructor, [dc][?] := [e0][10]
- Copy constructor, [da][?] := [de][1]
- Constructor, [d8][?] := [?][0].
- f = a + z + a + z + c + z + z + z;
- Addition (copy), return [e2][1] + [d8][0]
- Copy constructor, [ca][?] := [e2][1]
- Increament, [ca][1] += [d8][0]
- Move constructor, [ee][?] <- [ca][1]
- Destructor, delete [ca][nan]
- Addition (temp), return [ee][1] + [e2][1]
- Increament, [ee][1] += [e2][1]
- Move constructor, [ec][?] <- [ee][2]
- Addition (temp), return [ec][2] + [d8][0]
- Increament, [ec][2] += [d8][0]
- Move constructor, [ea][?] <- [ec][2]
- Addition (temp), return [ea][2] + [de][1]
- Increament, [ea][2] += [de][1]
- Move constructor, [e8][?] <- [ea][3]
- Addition (temp), return [e8][3] + [d8][0]
- Increament, [e8][3] += [d8][0]
- Move constructor, [e6][?] <- [e8][3]
- Addition (temp), return [e6][3] + [d8][0]
- Increament, [e6][3] += [d8][0]
- Move constructor, [e4][?] <- [e6][3]
- Addition (temp), return [e4][3] + [d8][0]
- Increament, [e4][3] += [d8][0]
- Move constructor, [d6][?] <- [e4][3]
- Destructor, delete [e4][nan]
- Destructor, delete [e6][nan]
- Destructor, delete [e8][nan]
- Destructor, delete [ea][nan]
- Destructor, delete [ec][nan]
- Destructor, delete [ee][nan]
- f += a;
- Increament, [d6][3] += [e2][1]
- e = b;
- Copy operator, [da][1] := [e0][10]
- a = c + a + f;
- Addition (copy), return [de][1] + [e2][1]
- Copy constructor, [ca][?] := [de][1]
- Increament, [ca][1] += [e2][1]
- Move constructor, [f6][?] <- [ca][2]
- Destructor, delete [ca][nan]
- Addition (temp), return [f6][2] + [d8][0]
- Increament, [f6][2] += [d8][0]
- Move constructor, [f4][?] <- [f6][2]
- Addition (temp), return [f4][2] + [d8][0]
- Increament, [f4][2] += [d8][0]
- Move constructor, [f2][?] <- [f4][2]
- Addition (temp), return [f2][2] + [d6][4]
- Increament, [f2][2] += [d6][4]
- Move constructor, [f0][?] <- [f2][6]
- Move operator, [e2][1] <- [f0][6]
- Destructor, delete [f0][nan]
- Destructor, delete [f2][nan]
- Destructor, delete [f4][nan]
- Destructor, delete [f6][nan]
- Destructor, delete [d6][nan]
- Destructor, delete [d8][nan]
- Destructor, delete [da][nan]
- Destructor, delete [dc][nan]
- Destructor, delete [de][nan]
- Destructor, delete [e0][nan]
- Destructor, delete [e2][nan]
- */
Add Comment
Please, Sign In to add comment