﻿# V823\. Decreased performance\. Object may be created in\-place in a container\. Consider replacing methods: 'insert' \-\> 'emplace', 'push\_\*' \-\> 'emplace\_\*'\.

The analyzer has detected a potentially inefficient method\. When inserting a temporary object into a container using the methods 'insert' / 'push\_\*', the object is constructed outside the container and then moved/copied into the container\.

On the other hand, the 'emplace' / 'emplace\_\*' methods allow you to eliminate the extra call of the move/copy constructor and create the object "in place" inside the container instead, perfectly passing the function's parameters to the constructor\.

The analyzer suggests the following replacements:

* insert \-\> emplace
* insert\_after \-\> emplace\_after
* push\_back \-\> emplace\_back
* push\_front \-\> emplace\_front

Example of inefficient code:

```cpp
std::string str { "Hello, World" };
std::vector<std::string> vec;
std::forward_list<std::string> forward_list;
std::list<std::string> list;
std::map<std::string, std::string> map;

....

vec.push_back(std::string { 3, 'A' });
forward_list.push_front(std::string { str.begin(), str.begin() + 6 });
list.push_front(str.substr(7));
list.push_back(std::string { "Hello, World" });
map.insert(std::pair<std::string, std::string> { "Hello", "World" });
```

Optimized version:

```cpp
std::vector<std::string> vec;
std::forward_list<std::string> forward_list;
std::list<std::string> list;
std::map<std::string, std::string> map;

....

vector.emplace_back(3, 'A');
forward_list.emplace_front(string.begin(), string.begin() + 6);
list.emplace_front(str.begin() + 7, str.end());
list.emplace_back("Hello, World");
map.emplace("Hello", "World");
```

In some cases such a replacement can lead to the loss of the base guarantee of exceptions security\. Let's consider the example:

```cpp
std::vector<std::unique_ptr<int>> vectUniqP;
vectUniqP.push_back(std::unique_ptr<int>(new int(0)));
auto *p = new int(1);
vectUniqP.push_back(std::unique_ptr<int>(p));
```

In this case, the 'push\_back' replacement for 'emplace\_back' can lead to memory leak if 'emplace\_back' throws the exception due to memory absence\. The analyzer doesn't issue warnings for such cases and doesn't suggest a replacement\. If the code is changed for the erroneous one, the analyzer will issue the [V1023](https://pvs-studio.com/en/docs/warnings/v1023/) warning\.

Sometimes replacing calls to 'insert' / 'push\_\*' with their 'emplace' / 'emplace\_\*' counterparts is not optimizing:

```cpp
std::string foo()
{
  std::string res;
  // doing some heavy stuff
  return res;
}

std::vector<std::string> vec;
....
vec.push_back(foo());
```

In this example, the 'emplace\_back' method will be as efficient as inserting the element using 'push\_back'\.  However, the warning will be still issued for the sake of consistency\. In all such cases, it will be sensible to make the replacement to keep the code consistent and avoid the reviewer's having to decide if 'emplace\*' should be used or not each time they read the code\. If you do not agree with this approach, you can view such warnings as false positives and suppress them\.

**Note\.** The recommendation described here should be approached reasonably, not formally\. For example, replacing

```cpp
widgets.push_back(Widget(foo, bar, baz));
// with
widgets.emplace_back(Widget(foo, bar, baz));
```

does not give any gain in the speed of the program\. Moreover, using 'emplace\_back' can slow down the code compilation speed\. For more information, see the article "[Don't blindly prefer emplace\_back to push\_back](https://quuxplusone.github.io/blog/2021/03/03/push-back-emplace-back/)"\. Our team would like to thank Arthur O'Dwyer for this publication\.