【问题标题】:Are objects inside rvalue referenced object, also rvalue referenced?右值引用的对象中的对象是否也被右值引用?
【发布时间】:2014-06-24 07:22:59
【问题描述】:

右值引用的对象内部的对象是否也被右值引用?

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

【问题讨论】:

    标签: c++ c++11 rvalue-reference


    【解决方案1】:

    是的,右值的成员本身就是右值。 DR 421 澄清了这一点

    但这在这里无关紧要:

    自动 &&in4 = b.a2;

    b 不是右值,它是左值(简单的经验法则:它有一个名称)。

    要恢复它在传递给函数时所具有的值类别,您需要 forward

    【讨论】:

    • 这个问题似乎得到了进一步的澄清here
    猜你喜欢
    • 1970-01-01
    • 2011-02-14
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    • 2015-08-28
    相关资源
    最近更新 更多