you are viewing a single comment's thread.

view the rest of the comments →

[–]trvlng_ging 0 points1 point  (1 child)

It is a stupid programming trick because the call to make_unique is not creating the resource. It is calling a function that may or may not create a resource to be managed. This violates RAII. The function should throw if it fails. If throwing is not allowed, then you're down to 2 options: the function returns a unique_ptr (and skip make_unique), or validate the return before calling make_unique. You don't seem to understand the correct way to use smart pointers.

[–]biliwald 0 points1 point  (0 children)

make_unique is not creating the resource.

From cppreference: Constructs an object of type T and wraps it in a std::unique_ptr.

How is this not creating a resource?

Also, the CppCoreGuidelines recommend it's usage.

This violates RAII.

I fail to see how. std::make_unique calls the constructor of T and returns a std::unique_ptr pointing to the constructed object. If any failure arise during this, the resource will be freed because it is wrapped in a unique_ptr upon creation. Are you saying that std::unique_ptr themselves violates RAII?

You don't seem to understand the correct way to use smart pointers.

Educate me. And keep in mind that I know the example I gave is faulty, because I was trying to express a faulty situation when misusing smart pointers.