Why pass an lvalue to a constructor that takes a const ref rvalue?


Lori

E.g:

#include <iostream>
#include <cstring>

using namespace std;

struct Square
{
    Square()
    {
        str = new char[10];
        strcpy(str, "Hello");
    }

    ~Square()
    {
        delete str;   
    }

    void print()
    {
        cout << "str = '" << str << "'" << endl;
    }

    char * str;    
};

class foo
{
public:
    typedef const Square & sqcref;
    typedef Square & sqref;
    foo(sqref && _ref)
    {
        fooStr = _ref.str;
        _ref.str = 0;
    }

    char * fooStr;
};

int main()
{
    Square s;
    s.print();
    foo f(s);
    s.print();
}

output:

str ='
Hello'str ='

Inside the constructor of a foo, it expects an rvalue to be Square &passed to a , but an Square &lvalue is passed in instead. Why did you do this?

mpark

This is due to reference collapse . sqref&&Here sqrefis the T&crash to T&.

https://wandbox.org/permlink/eWeYLdI3l0vSpNvo

Related


Parameters to pass Lvalue to RValue

Rajeshwar I wonder how is this possible? template<typename T> void Test(T&& arg) { arg = 14; } int a = 23; Test(a); My problem is that the function Test expects an argument of type Rvalue, but it also seems to accept an argument of type lvalue. why is t

Parameters to pass Lvalue to RValue

Rajeshwar I wonder how is this possible? template<typename T> void Test(T&& arg) { arg = 14; } int a = 23; Test(a); My problem is that the function Test expects an argument of type Rvalue, but it also seems to accept an argument of type lvalue. why is t

cannot pass lvalue to construct rvalue

Tinggo Can anyone help explain why lvalues can UseStringnot be accepted as arguments in my example below? I know that Stringin my case a temp is created , but I can't explain why it has to accept an rvalue here. #include <utility> using namespace std; struct

cannot pass lvalue to construct rvalue

Tinggo Can anyone help explain why lvalues can UseStringnot be accepted as arguments in my example below? I know that Stringin my case a temp is created , but I can't explain why it has to accept an rvalue here. #include <utility> using namespace std; struct

cannot pass lvalue to construct rvalue

Tinggo Can anyone help explain why lvalues can UseStringnot be accepted as arguments in my example below? I know that Stringin my case a temp is created , but I can't explain why it has to accept an rvalue here. #include <utility> using namespace std; struct

cannot pass lvalue to construct rvalue

Tinggo Can anyone help explain why lvalues can UseStringnot be accepted as arguments in my example below? I know that Stringin my case a temp is created , but I can't explain why it has to accept an rvalue here. #include <utility> using namespace std; struct

cannot pass lvalue to construct rvalue

Tinggo Can anyone help explain why lvalues can UseStringnot be accepted as arguments in my example below? I know that Stringin my case a temp is created , but I can't explain why it has to accept an rvalue here. #include <utility> using namespace std; struct