C++11 rvalue reference vs const reference

后端 未结 5 1205
你的背包
你的背包 2020-12-06 02:54

This may be obvious but I think it is something difficult to me. Given this:

void test(std::string&&) { }

std::string x{\"test\"};
test(std::move(x)         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-06 03:20

    In simple terms:

    • && can bind to non-const rvalues (prvalues and xvalues)
    • const && can bind to rvalues (const and non-const)
    • & can bind to non-const lvalues
    • const & can bind to rvalues (prvalues and xvalues) and lvalues (const and non-const for each). A.k.a. to anything.

提交回复
热议问题