Re: std::pair, segmentation fault, why?
On 19 Sep., 14:33, "Kai Wen" wrote:
"Bo Persson" wrote:
In addition to what Alf said, you probably shouldn't return rvalue
references except in very special cases (like forwarding functions). Ju=
st
return the local or temporary by value, and let the receiving object's
move constructor or move assignment pick that up. The function return
value is an rvalue anyway.
But if we write these code, these seems not beautiful:
Obj* pa = factory_make_big_obj(); // we must remember to delete pa
Obj b;
factory_make_big_obj(b); // ugly (there pass b by a=
refrence)
Obj b = factory_make_big_obj(); // if support "move", it's beaut=
iful
If Obj is a move-enabled type you don't have to do anything special in
your factory function. That's the beauty of it:
Obj factory_make_big_obj()
{
...
Obj result...
...
return result;
}
...
Obj b = factory_make_big_obj();
It's all taken care of automatically. No unnecessary copying will be
done here. That's guaranteed! You don't have to write your function to
return an rvalue reference. In fact you *should* not do so.
If you want polymorphism and/or deal with a type that is NOT
efficiently movable you can also write things like this in C++0x:
std::unique_ptr<Obj> factory_make_big_obj()
{
...
unique_ptr<Obj> result...
...
return result;
}
...
unique_ptr<Obj> up = factory_make_big_obj();
If you later decide to share ownership of the pointed-to object you
can use a conversion from unique_ptr to shared_ptr or directly create
a shared_ptr using std::make_shared.
Anyhow, there is no need here to explicitly use rvalue references or
even std::move here. In most cases, rvalue references are just needed
by class authors so they can provide a move constructor to make their
types efficiently movable. And I didn't use std::move here because
it's not necessary. If you return a function-local object like
'result' (see above) the compiler will try the following:
1. Elide the copy/move and be done.
2. If that's not possible, look for a move constructor and use it
for constructing the return value.
3. If there is no move constructor, use the copy constructor
for constructing the return value.
Cheers!
SG