3
class MyClass {
  public:
      MyClass()
      {
          std::cout << "default constructor\n";
      }
      MyClass(MyClass& a)
      {
          std::cout << "copy constructor\n";
      }

      MyClass(MyClass&& b)
      {
          std::cout << "move constructor\n";
      } 
};  

void test(MyClass&& temp)
{
    MyClass a(MyClass{}); // calls MOVE constructor as expected
    MyClass b(temp); // calls COPY constructor... not expected...? 
}

int main()
{
    test(MyClass{});
    return 0;
}

For the above code, I expected both object creations in test() to call move constructor because b is a r-value reference type (MyClass&&).

However, passing b to the MyClasss constructor does not call move constructor as expected, but calls copy constructor instead.

Why does second case calls copy constructor even if the passed argument is of type MyClass&& (r-value reference)???

I am using gcc 5.2.1. To reproduce my results, you must pass -fno-elide-constructors option to gcc to disable copy elision optimization.


void test(MyClass&& temp)
{
    if (std::is_rvalue_reference<decltype(temp)>::value)
        std::cout << "temp is rvalue reference!!\n";
    else
       std::cout << "temp is NOT rvalue!!\n";
}

Above code prints out "temp is rvalue reference" even if temp is named.

temp's type is rvalue reference type.

SHH
  • 3,226
  • 1
  • 28
  • 41
  • 1
    Expression `temp` evaluates to an lvalue refernce, not an rvalue reference. The fact that you declared `temp` it as rvalue reference does not matter. A *named* reference is always an lvalue. – AnT stands with Russia Jul 16 '16 at 00:47
  • @AnT Do you have any reference in C++ standard documents? I tried really hard to look for any reference to this rule, but I could not find one... – SHH Jul 16 '16 at 01:05
  • 2
    See the beginning of **5 Expressions**. It is all in 5/5, 5/6 and 5/7. See Note 5/7 especially, which basically states exactly that. Also the linked "duplicate" has a link to a Thomas Becker's article on the topic. – AnT stands with Russia Jul 16 '16 at 01:22

1 Answers1

4
void test(MyClass&& temp)
{
    MyClass a(MyClass{}); // calls MOVE constructor as expected
    MyClass b(temp); // calls COPY constructor... not expected...? 
}

That is because, temp (since it has a name,) is an lvalue reference to an rvalue. To treat as an rvalue at any point, call on std::move

void test(MyClass&& temp)
{
    MyClass a(MyClass{}); // calls MOVE constructor as expected
    MyClass b(std::move(temp)); // calls MOVE constructor... :-) 
}
WhiZTiM
  • 21,207
  • 4
  • 43
  • 68
  • 'an lvalue reference to an rvalue' ...How can rvalue have lvalue reference? That's beyond my understanding. – Youda008 Apr 02 '17 at 10:52
  • I know it's subtle. A variable usage in code is actually variable expression with one operand and no operator and therefore has the lvalue/rvalue property - variable expressions are lvalues. The twist is that any declared variable persists in it's scope until it is out of scope and therefore is not 'ephemeral' like a temporary. The rvalue reference is a variable, on it's own, has a scope in your function test() and it's usage it a variable expression. – Peter Jul 19 '19 at 18:33