<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/108407>108407</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Memory allocation elision for std::vector cases
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
apolukhin
</td>
</tr>
</table>
<pre>
Code close to the following was noted in many user applications:
```cpp
bool test1(const std::vector<std::string_view>& in) {
return in == std::vector<std::string_view>{"*"};
}
```
Here people wish to make sure that the vector contains only a single "*" element. In other words they assume that the above code snippet would be optimized to something like:
```cpp
bool test2(const std::vector<std::string_view>& in) {
return in.size() == 1 && in[0] == "*";
}
```
Unfortunately that does not happen: https://godbolt.org/z/8WThj5Mrq
Note that all the functions are inlined however the new+delete are not elided.
Minimized example: https://godbolt.org/z/T8a8a8nxq
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVMGO2zYQ_RrqMohBUZItH3Sw4xrtYXtK0WNBiWOLWYqjkCMr3q8vKLmb7V66BQIYpMXhvHl474E6Rnv1iI2ojqI6ZXrinkKjR3LTc2991pK5N5_JIHSOIgITcI9wIedotv4Ks47gidGA9TBof4cpYgA9js52mi35KIqDkCchD2Ir1183jutJS-SAMXIuVN2RjwyRTWooDjfsmIIoPr-eRA7WX_-6WZxF8YtQW7BeqD2I3XGFAwAIyFPwiY0oTqI4_Q_A3VEoJdQhrbuTKB6o6f-_-a-fv2JAGJFGhzDb2CdxBv2MEKeAwL3mRat1LHTkWVsfgby7g4Zo_dUhvE4EdDig5w385oG4xwAzBRMTxh10jNPwBlS3dEPokjHR23FEhpkmZ6BFoJHtYF_QJEKRBuQ-OeXsM37ECvVzrHj1YRPtCwpVL-XVkhyE2j6aqqMU1emfyg8D_kv9df3DXyjw5DWju6_yGMIlktDrcUQvigP0zOMSQ3UW6nwl05LjDYWrUOcXoc71n1_6r9VT-PYW-nfih-DauTX1k--WRIMOCNY769FATzPeMCw3PM5CHQ06ZFwuJR7orEGzeYv9ZP3DIvyuh9HhR1h-qXWta__9W2aawuyLvc6wyXdqm1eV3JZZ3-hOF22psdLdRRVlVRWXciuNaU1Zqv1FZrZRUpVyn6u8KnJZbFTVtqhrozu5zVUnRSlx0NZtnLsNaXZmY5ywyWVdyl3mdIsuLq-FUh5nWKrJruqUhSY1fWqnaxSldDZy_AHDlh02TzhQuCdBaX0ckjgx7RcK7_MGnY4Ysym45p00lvup3XQ0CHVOEx7bpzHQV-xYqPPCKwp1fhC_NervAAAA___z2Ip6">