Synopsis: | Use std::make_shared instead of constructing a shared_ptr from a raw pointer |
Language: | C++ |
Severity Level: | 2 |
Category: | Object Allocation |
Description: |
There are a couple of advantages to prefer std::make_shared to the ordinary way of creating a shared pointer.
Example (wrong): std::shared_ptr<Object> ptr(new Object("foo")); Example (correct): std::shared_ptr<Object> ptr = std::make_shared<Object>("foo"); Example (correct, even better): auto ptr = std::make_shared<Object>("foo"); |