是否在rvalue引用对象里面的对象,也引用了rvalue? [英] Are objects inside rvalue referenced object, also rvalue referenced?

查看:183
本文介绍了是否在rvalue引用对象里面的对象,也引用了rvalue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对象是否在rvalue引用对象内,也引用了rvalue?

  struct A {
}

struct B {
A a2;
};

// template< class B>
void test(B&& b){

// 1.这是正确的方法吗?
auto&& in3 = std :: forward& b(b).a2;
std :: cout<< std :: is_rvalue_reference< decltype(in3)> :: value;
// return true

// 2.或者这个?
auto&& in4 = b.a2;
std :: cout<< std :: is_rvalue_reference< decltype(in4)> :: value;
// return false
}

test(B());

http://coliru.stacked-crooked.com/a/bcf0f7dc4cc0440e

解决方案

是的,右价的成员本身是价值。这一点已通过 DR 421 澄清。



但这在这里是无关的:


auto&& in4 = b.a2;


b 不是右值,它是一个左值(简单的经验法则:



要恢复传递给函数时所需的值类别,需要 forward

Are objects inside rvalue referenced object, also rvalue referenced?

struct A{
};

struct B{
   A a2;
};

//template<class B>
void test(B &&b){

    // 1. Is this the correct way?
    auto &&in3 = std::forward<B>(b).a2;
    std::cout << std::is_rvalue_reference<decltype(in3)>::value;
    // return true

    // 2. or this?
    auto &&in4 = b.a2;
    std::cout << std::is_rvalue_reference<decltype(in4)>::value;    
    // return false        
}

test(B());

http://coliru.stacked-crooked.com/a/bcf0f7dc4cc0440e

解决方案

Yes, members of rvalues are themselves rvalues. This was clarified by DR 421

But that is irrelevant here:

auto &&in4 = b.a2;

b is not an rvalue, it's an lvalue (simple rule of thumb: it has a name).

To restore the value category it had when passed to the function you need to forward it

这篇关于是否在rvalue引用对象里面的对象,也引用了rvalue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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