TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
C++ Coding Standard
Search

Rule:  OAL#013Checked automatically with code checker

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.

  • Better readibility (see example below)
  • Less memory consumption
  • Better exception-safety

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");