Storing rvalue references: should this work?

旧巷老猫 提交于 2019-12-07 11:03:55

问题


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

回答1:


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).



来源:https://stackoverflow.com/questions/20586070/storing-rvalue-references-should-this-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!