Storing rvalue references: should this work?

Kristian D'Amato

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
Cheers and hth. - Alf

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Dynamically reference to $this should not work, but it does

来自分类Dev

Should implicit conversion work in the context of a template argument?

来自分类Dev

书中“ rvalue”和“ rvalue reference”之间的混淆

来自分类Dev

Lvalue to rvalue reference binding

来自分类Dev

nullptr rvalue如何

来自分类Dev

std :: bind和rvalue参考

来自分类Dev

std :: thread和rvalue参考

来自分类Dev

Storing binary blobs in Cassandra

来自分类Dev

Storing threads in a hash

来自分类Dev

Confusion about rvalue reference: what is its type?

来自分类Dev

使用rvalue / lvalue了解模板参数推导

来自分类Dev

将Lvalue传递给RValue的参数

来自分类Dev

为什么Rvalue无法绑定Lvalue引用?

来自分类Dev

移动构造函数-是new A()rvalue吗?

来自分类Dev

Rvalue成员的确也是Rvalues吗?

来自分类Dev

强制转换后是Rvalue还是Lvalue

来自分类Dev

Storing intermediate values in a numpy array

来自分类Dev

Storing and retrieving an array in a PHP cookie

来自分类Dev

Storing different density drawables in the folder

来自分类Dev

__init __。storing是什么信息?

来自分类Dev

Storing HTML markup in a PHP string

来自分类Dev

Axios not storing Django session cookie

来自分类Dev

带有rvalue引用参数的模板函数中的std :: is_rvalue_reference

来自分类Dev

将从函数返回的Rvalue分配给另一个Rvalue

来自分类Dev

Cross-service references in DB

来自分类Dev

Structured References: Absolute and Relative addressing

来自分类Dev

如何从C ++中的rvalue和lvalue参数移开?

来自分类Dev

可以将std :: map创建为rvalue吗?

来自分类Dev

C ++基于范围的for循环遍历valarray rvalue不起作用