Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do rvalue references allow implicit conversions?

Is the following code legal?

std::string&& x = "hello world";

g++ 4.5.0 compiles this code without any problems.

like image 831
fredoverflow Avatar asked Jun 26 '10 13:06

fredoverflow


People also ask

Can you pass an lvalue to an rvalue reference?

By overloading a function to take a const lvalue reference or an rvalue reference, you can write code that distinguishes between non-modifiable objects (lvalues) and modifiable temporary values (rvalues). You can pass an object to a function that takes an rvalue reference unless the object is marked as const .

What are rvalue references good for?

Rvalue references is a small technical extension to the C++ language. Rvalue references allow programmers to avoid logically unnecessary copying and to provide perfect forwarding functions. They are primarily meant to aid in the design of higer performance and more robust libraries.

How do you pass rvalue reference to a function?

If you want pass parameter as rvalue reference,use std::move() or just pass rvalue to your function.

How do I get rvalue reference?

An rvalue reference is formed by placing an && after some type. An rvalue reference behaves just like an lvalue reference except that it can bind to a temporary (an rvalue), whereas you can not bind a (non const) lvalue reference to an rvalue.


1 Answers

This is discussed on usenet currently. See Rvalue reference example in 8.5/3 correct or wrong?.

It's not legal.

like image 130
Johannes Schaub - litb Avatar answered Oct 13 '22 17:10

Johannes Schaub - litb