Member gains ownership of the parameter

I see two reasonable solutions to the problem of the member owning the parameter:

Foo::Foo(std::unique_ptr<int> parameter) : member(std::move(parameter)) { } Bar::Bar(std::unique_ptr<int> parameter) { member.swap(parameter); } 

Which one is more idiomatic, easier to understand, easier to debug, easier to maintain, etc.?

Are there any additional solutions to the problem that I missed?

+6
source share
1 answer

Always prefer a list of initializers. In addition, it covers types that can be moved but not replaced, for example, by types that may have to perform expensive operations to build by default, or indeed, types that simply cannot be built by default.

+12
source

Source: https://habr.com/ru/post/920037/


All Articles