0

Lets look into this snippet and as expected I get In && printed. However, instead of z = x+y if I had written S z(x + y) I don't get the call to "In move". Instead the temporary created inside operator+ seems to be what z is. Maybe the compiler is optimizing it but I don't quite get it.

#include <algorithm>
#include <iostream>
using namespace std;

struct S {
  S(int N1) : N(N1) {
    cout << "In orig " << N << "\n";
  }

  S(S& s) {
    N = s.N;
    cout << "In copy\n";
  }

  S(S&& s) {
    N = s.N;
    cout << "In move\n";
  }

  S const & operator=(S&& s) {
    cout << "In &&\n";
  }

  S operator+(S const & s) {
    cout << "In +\n";
    return S(N + s.N);
  }

  int N;
};

int main()
{
  S x(33), y(44);
  S z = 99; // instead of this line and next try S z(x + y); --> no move constructor??
  z = (x + y);
}
Fanatic23
  • 3,378
  • 2
  • 28
  • 51

0 Answers0