Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lvalue reference constructor is called instead of rvalue reference constructor

There is this code:

#include <iostream>

class F {
public:
   F() = default;
   F(F&&) {
      std::cout << "F(F&&)" << std::endl;
   }
   F(F&) {
      std::cout << "F(F&)" << std::endl;
   }
};

class G {
   F f_;
public:
   G(F&& f) : f_(f) {
      std::cout << "G()" << std::endl;
   }
};

int main(){
   G g = F();
   return 0;
}

The output is:

F(F&)
G()

Why F(F&) constructor is called instead of F(F&&) constructor in constructor of class G? The parameter for constructor of class G is F&& f which is rvalue reference but constructor for lvalue reference is called.

like image 900
scdmb Avatar asked Mar 09 '13 19:03

scdmb


People also ask

What is the difference between lvalue and rvalue references?

“l-value” refers to a memory location that identifies an object. “r-value” refers to the data value that is stored at some address in memory. References in C++ are nothing but the alternative to the already existing variable. They are declared using the '&' before the name of the variable.

Can lvalue bind to rvalue reference?

An lvalue reference can bind to an lvalue, but not to an rvalue.

What is lvalue and rvalue in CPP?

An lvalue (locator value) represents an object that occupies some identifiable location in memory (i.e. has an address). rvalues are defined by exclusion. Every expression is either an lvalue or an rvalue, so, an rvalue is an expression that does not represent an object occupying some identifiable location in memory.

Can rvalue be copied?

Yes, rvalues are moved, lvalues are copied. But when there's no according move operation, rvalues are copied as well.


1 Answers

Why F(F&) constructor is called instead of F(F&&) constructor in constructor of class G?

Because f is an lvalue. Even though it is bound to an rvalue, and its type is rvalue reference to F, it is also a named variable. That makes it an lvalue. Don't forget that the value category of an object is not determined by its type, and vice versa.

When you pass an lvalue to a function, only lvalue references can be bound to it. You should change your code as follows if you want to catch rvalues only:

class G {
    F f_;
public:
    G(F&& f) : f_(std::move(f)) {
       std::cout << "G()" << std::endl;
    }
};

Alternatively, you could use std::forward<>(), which is equivalent in this case, but makes your intent of forwarding f even clearer:

class G {
    F f_;
public:
    G(F&& f) : f_(std::forward<F>(f)) {
       std::cout << "G()" << std::endl;
    }
};

Now this last definition is easy to extend so that both lvalues and rvalues of type F can be bound to the parameter f:

class G {
    F f_;
public:
    template<typename F>
    G(F&& f) : f_(std::forward<F>(f)) {
       std::cout << "G()" << std::endl;
    }
};

This allows, for instance, to construct an instance of G this way:

F f;
G g(f); // Would not be possible with a constructor accepting only rvalues

This last version has a caveat though: your constructor will basically work as a copy-constructor as well, so you might want to explicitly define all the possible copy constructors to avoid awkward situations:

class G {
    F f_;
public:
    template<typename F>
    G(F&& f) : f_(std::forward<F>(f)) {
       std::cout << "G()" << std::endl;
    }
    G(G const&) = default;
    G(G&); // Must be defaulted out-of-class because of the reference to non-const
};

G::G(G&) = default;

Since non-template functions are preferred over instantiations of function templates, the copy constructor will be selected when constructing a G object from another G object. The same applies, of course, to the move constructor. This is left as an exercise.

like image 155
Andy Prowl Avatar answered Oct 14 '22 05:10

Andy Prowl