push_back of a temporary object
ID |
c.performance.use_emplace |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Inefficient Call |
Language |
C / C++ |
Description
push_back is called with a freshly constructed temporary, which is built and then moved (or copied) into the container. emplace_back(args…) constructs the element in place and avoids the temporary. (A call whose argument is a plain function call rather than a constructor is a false positive; verify the argument really builds a new object.)
Rationale
push_back is called with a freshly constructed temporary, which is built and then moved (or copied) into the container. emplace_back(args…) constructs the element in place and avoids the temporary. (A call whose argument is a plain function call rather than a constructor is a false positive; verify the argument really builds a new object.)
The following code illustrates the pattern detected by this rule:
void f(std::vector<Foo> v, Foo existing) {
// FLAGGED: push_back of a temporary object
v.push_back(Foo(1, 2));