Why capturing return value in rvalue reference has more assembly instructions than capturing it by value in C++

  arm, assembly, c++, c++14, gcc

I was reading this SO question and found that best practice is to return by a value. so I ran below two examples in godbolt with ARM gcc 10.2(linux) compiler to get assembly instructions for both

1.

#include <iostream>
#include <vector>

std::vector<int> return_vector(void)
{
    std::vector<int> tmp {1,2,3,4,5};
    return tmp;
}

int main()
{
   std::vector<int> && rval_ref = return_vector();
   while(1){}
  return 0;
}
  1. Almost same but without &&
#include <iostream>
#include <vector>

std::vector<int> return_vector(void)
{
    std::vector<int> tmp {1,2,3,4,5};
    return tmp;
}

int main()
{
   std::vector<int>  rval_ref = return_vector();
   while(1){}
  return 0;
}

now code with rvalue && has more instructions compared to code without rvalue reference. shouldn’t they be same ?

Below are extra assembly instructions for code with rvalue reference
(left is code with rvalue ref && and right is code without it.)

enter image description here

Source: Windows Questions C++

LEAVE A COMMENT