void testRR(std::string &A);
void testRR(std::string &&A);

Which one is better? && is more flexible than &, should I choose && rather than &?

Thanks

Edited 5 Years Ago by stereomatching: n/a

&& is more flexible than &

You think so?

Which one is better?

Gosh, I don't know that one.But what I know (up until now) is, those are two different things(though their result may coincide sometimes).The '&' operator is a 'bitwise operator' and returns the result of the operation between the two operands whereas the '&&' is called a logical operator and returns True of false(1 or 0) only.

Edited 5 Years Ago by D33wakar: n/a

void testRR(std::string &A);
void testRR(std::string &&A);

Which one is better?

You should prefer lvalue references to non-const for things you want to modify and lvalue references to const for things you can't or don't want to modify (rvalues are neatly handled here). In other words, my recommendation is that you keep doing things the way they were done before C++0x, and let others blaze the trail with rvalue reference usage. :)

&& is more flexible than &

How do you figure?

Because

A a;
A& a_ref1 = a;  // an lvalue reference

A a;
A&& a_ref2 = a;  // an rvalue reference

A&  a_ref3 = A();  // Error!
A&& a_ref4 = A();  // Ok

that means

void testRR(std::string &&A);

should be able to accept something like

std::string A;
testRR(A);

or

testRR("pppp");

but gcc4.6.1(mingw) wouldn't accept testRR(A) except testRR(std::move(A))
vc2010 wouldn't accept testRR("aaa")(gcc can)

Please correct me if I commit any error
Thanks

Please correct me if I commit any error

You can't implicitly bind an lvalue to an rvalue reference any more than you can bind an rvalue to a non-const lvalue reference.

Edited 5 Years Ago by Narue: n/a

got it, the code like this

A&& a_ref2 = a;  // an rvalue reference

Is not valid in the current standard?
It was a proposal for rvalue reference?
That means we have to overload it if we want to bind rvalue to testRR

testRR("pppp");

can't work on vc2010 but gcc4.6.1(minGW) can, which one follow the standard?
Thank you very much

This article has been dead for over six months. Start a new discussion instead.