Item 17: Store newed Objects in Smart Pointers in Standalone Statements

Because the evaluation order of function designator, arguments, and subexpression within arguments is unspecified.

This statement

bar(std::shared_ptr(new Foo), baz());

…could be evaluated in this order:

  • new Foo
  • baz()
  • std::shared_ptr(...)
  • bar(...)

And if baz() throws an exception, the newed Foo object is leaked.

Creative Commons License
This blog by Che-Liang Chiou is licensed under a Creative Commons Attribution 4.0 International License.