The only difference is style. Since use of operator new
is now considered old fashioned std::make_unique
is recommended.
For compiler there is no difference.
By the way is should be:
A::A() : m_b{std::make_unique<B>()} {}
Also see
CppCoreGuidelines/CppCoreGuidelines.md at master · isocpp/CppCoreGuidelines
C.150: Use make_unique()
to construct objects owned by unique_ptr
s
Reason
make_unique
gives a more concise statement of the construction. It also ensures exception safety in complex expressions.
Example
unique_ptr<Foo> p {new Foo{7}}; // OK: but repetitive
auto q = make_unique<Foo>(7); // Better: no repetition of Foo
// Not exception-safe: the compiler may interleave the computations of arguments as follows:
//
// 1. allocate memory for Foo,
// 2. construct Foo,
// 3. call bar,
// 4. construct unique_ptr<Foo>.
//
// If bar throws, Foo will not be destroyed, and the memory-allocated for it will leak.
f(unique_ptr<Foo>(new Foo()), bar());
// Exception-safe: calls to functions are never interleaved.
f(make_unique<Foo>(), bar());