Use of new instead of make_unique
Description
This defect occurs when you use the new operator instead of
std::make_unique() to initialize or reset a unique_ptr
from the standard library or Boost. For example, this code initializes a
unique_ptr
u_ptr2str by using the new
operator.
std::unique_ptr<std::string> u_ptr2str(new std::string()); Risk
Using the new operator provides no benefit over using
std::make_unique().
Fix
To fix this defect, use std::make_unique() to initialize or reset
unique_ptr objects. Using std::make_unique()
avoids the drawbacks of the new operator..
Examples
Result Information
| Group: Good Practice |
| Language: C++ |
| Default: Off |
Command-Line Syntax:
MISSING_MAKE_UNIQUE
|
| Impact: Low |
PQL Name:
std.defects.MISSING_MAKE_UNIQUE
|
Version History
Introduced in R2026a
See Also
Find defects
(-checkers) | Expensive local
variable copy | Reference to un-named
temporary
Topics
- Interpret Polyspace Bug Finder Results in Polyspace Platform User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Polyspace Results Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)
- Expensive-to-Read Objects in Bug Finder