Question

I'm testing my understanding of lvalue and rvalue references by intentionally trying to break things. So say there is this struct:

struct FooBar
{
    FooBar(int&& number) : rNumber(number)
    {

    }

    int& rNumber;
};

and I create an instance FooBar obj(5). Every attempt to read the reference variable returns the right result (5). The same happens if I use const int& instead of int&&.

I noticed that replacing int with std::string and reading the reference returns an empty string, so I suspect it gives undefined behaviour. Is this so? And if so, why does it work with integers?

Update: I'm creating the instance and reading it like this:

FooBar obj(5);
//FooBar obj("Hello"); // For strings...

std::cout << obj.rNumber << std::endl;

Update 2: It also works if you pass a user-defined type, like this:

struct GooBar
{
public:
    GooBar(int number) : itsNumber(number) 
    {
        std::cout << "In constructor..." << std::endl;
    }

    GooBar(const GooBar& rhs) = delete;
    GooBar(GooBar&& rhs) = delete;

    ~GooBar() 
    {
        std::cout << "In destructor..." << std::endl;
    }

    int itsNumber;
};


struct FooBar
{
    FooBar(GooBar&& number) : rNumber(number)
    {

    }

    GooBar& rNumber;
};

and then creating an instance and reading it like so:

FooBar obj(GooBar(5));

std::cout << obj.rNumber.itsNumber << std::endl;

I think this is interesting, because it gives the following output:

In constructor...
In destructor...
5
Was it helpful?

Solution

With an integer literal as actual argument the compiler may pass a reference to a statically allocated instance.

With a std::string formal argument and a string literal as actual argument, the instance is created in the call, and is destroyed at the end of the call.

In both cases it's Undefined Behavior.


It's not clear how you call this though: you forgot to include that crucial information (as the question is as at time I'm writing this).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top