存储右值引用:这应该工作吗? [英] Storing rvalue references: should this work?

查看:169
本文介绍了存储右值引用:这应该工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过有意尝试打破事物来测试我对左值和右值引用的理解。所以说有这个结构:

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

}

int& rNumber;
};

并创建一个实例 FooBar obj(5)。每次尝试读取引用变量都会返回正确的结果(5)。如果我使用 const int& 而不是 int&&& ,也会发生同样的情况。



我注意到用 std :: string 替换 int 并读取引用返回一个空字符串,所以我怀疑它给出未定义的行为。是这样吗?

更新:我正在创建的实例,并像这样阅读:

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

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

更新2:如果您传递用户定义的类型,像这样:

  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& amp; amp; amp; number):rNumber(number)
{

}

GooBar& rNumber;
};

,然后创建一个实例并像这样读:

  FooBar obj(GooBar(5)); 

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

我认为这很有趣,因为它给出以下输出:

 在构造函数... 
在析构函数...
5


解决方案

使用整数文字作为实参,编译器可以传递静态分配的实例的引用。



使用 std :: string 形式参数和字符串文字作为实际参数,



在这两种情况下都是未定义的行为。






你不清楚你是怎么称呼这个的:你忘了包含那些重要的信息(因为问题是在写这个时候)。


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

解决方案

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

这篇关于存储右值引用:这应该工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆