Feature Request: Support emplacing multiple elements with the same constructor arguments into vectors
- Dominant language
- C++
- Stars
- 127
- Forks
- 126
- Avg merge
- 18h 53m
- Merged PRs (30d)
- 1
Description
Consider a case when we have a non-default-constructibe type, which we want to store in a vector (any flavor of the supported by Boost.Container). In order to allocate N elements in the vector, one has to create one template object, which is then used to copy-construct elements:
```
class A
{
public:
explicit A(int, float);
};
boost::container::vector vec;
vec.resize(10, A(42, 3.14f));
```
This can be problematic if `A` is not copy-constructible or is expensive to copy. A better solution would be to directly emplace N elements from the user-provided constructor arguments:
```
vec.resize_emplace(10, 42, 3.14f); // calls A(42, 3.14f) 10 times
```
This construction would be useful also because there is no easy way to achieve this with raw arrays, statically or dynamically allocated. Currently, one has to separately allocate storage and inplace-construct elements, with the careful fallback in case of exception, and do the reverse on destruction.
The same extension could be made for `insert`:
```
vec.insert_emplace(vec.begin(), 10, 42, 3.14f); // moves elements as necessary and calls A(42, 3.14f) 10 times
```
This could be emulated by a loop of `emplace` calls, but this is less efficient than the dedicated methods. In particular, the `emplace` loop would unnecessarily move elements and reallocate memory.
PS: The method names are given for illustration, I'm not attached to them.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.