2
2番目の呼び出しを有効にするためのインターフェイスの変更はありますか?パラメータとしてのstd :: unique_ptrの適切なコピーセマンティクス
私は物事をそのまま残すべきですか?
私は、最初のケースの余分な構成が意図的にそのように設計されているので、所有権が移転されていることは明らかです。
#include <memory>
struct Bar { };
typedef std::unique_ptr<Bar> UPBar;
void foo1(UPBar p) { }
void foo2(UPBar p) { foo1(move(p)); }
void foo3(UPBar p) { foo2(move(p)); }
void foo4(UPBar p) { foo3(move(p)); }
int main(int argc, char** argv)
{
UPBar p(new Bar);
foo4(move(p)); // ok, but requires an extra construction vs line below
foo4(new Bar); // fails: any modification to get this to work?
return 0;
}
2番目の質問:私は右辺値参照-(& &)に渡されたすべてのパラメータを変更する場合は、そうすることで任意の欠点はありますか?実際には、すべての私のstd::unique_ptr<>
パラメータがRValue-Referencesによって渡されることを保証すべきですか?
- tyvmを。優れた第2のチップ - タイ。私はすべてのfooX()関数を値渡しの代わりにRHR/&&を受け入れるように変更する必要がありますか? – kfmfe04
@ kfmfe04:いいえ。 'unique_ptr'を常に値で取ってください。この質問を参照してください:http://stackoverflow.com/questions/8114276/how-do-i-pass-a-unique-ptr-argument-to-a-constructor-or-a-function。これらの引数はコンストラクタに転送されるため、この関数では '&& 'で引数を取ります。 –
いいリンクのために+1時間 – kfmfe04