dimanche 20 décembre 2020

RVO and move constructor

#include <bits/stdc++.h>

struct Row
{
  int a;
  Row() { puts("default"); }
  Row(const Row &other) { puts("copy"); }
  Row(Row &&other) { puts("move"); }
  Row(int) { puts("conv. c'tor"); }
};

Row return_row()
{

  Row r(6);
  Row second = r;
  return second; // move happens here
}

int main()
{
  Row x = return_row();
}

Why it prints move instead of copy since I am copying lvalue? I have turned of elide constructors flag still it prints move. Is it because of RVO/ NRVO ? If it is, can someone explain in which scenario RVO/NRVO happens?

Aucun commentaire:

Enregistrer un commentaire