195 questions
0
votes
1
answer
103
views
Using tbb::parallel_for with reserve and emplace does not copy all elements between std::vectors
I am trying to populate a std::vector container using another std::vector and tbb::parallel_for method as shown in the following code snippet:
#include <tbb/parallel_for.h>
#include <eigen3/...
1
vote
0
answers
119
views
Why can't I do emplace_back(...) instead of push_back({...})? [duplicate]
I thought the point of emplace_back was to forward arguments to the constructor instead of constructing a temporary object, but here it gives me a long nasty compiler error I can't interpret, whereas ...
0
votes
0
answers
91
views
Is there a caveat for using placement new instead of move assignment for a emplace_back imitation on a static array
I was trying to implement an emplace_back method for a statically-sized array (for fun).
I came across the problem of whether I should use placement-new or move assignment for this emplace back method....
2
votes
2
answers
90
views
How to use std::map::emplace to add a default constructed value when it is not movable
This compiles:
#include <map>
struct S {
int val = 0;
S() = default;
S(S&&) = default;
};
int main() {
std::map<int, S> values;
values.emplace(42, S{});
}
...
2
votes
1
answer
122
views
C++ how to use emplace in std::map using forwarding argument
With an std::vector I've managed to use emplace_back by forwarding the arguments, as in this code:
#include<iostream>
#include<map>
#include <string>
#include <utility>
#...
2
votes
1
answer
91
views
Implementation of STL Emplace( ) Method for a Template Queue ADT in Cpp
I got a my implementation of STL queue data structure in C++ with a template class. I'm facing a problem that is implementation of emplace ( ) method for this template ADT. In emplace we use receiving ...
3
votes
1
answer
115
views
c++ std::map::emplace() doesn't work but operator[] is ok
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <unordered_map>
#include <unordered_set>
using namespace std;
// 双向链表
struct ...
1
vote
0
answers
79
views
C++ std::map emplace_hint - long processing time when increasing the iterator
I am trying to understand the sample code at the emplace_hint() documentation: https://en.cppreference.com/w/cpp/container/map/emplace_hint
Especially the following function:
std::size_t ...
1
vote
2
answers
94
views
Scope of an entry added to a c++ set?
Let's say I have a globally scoped set defined as follows:
std::set<Person> people;
I also have a function where I create an instance of the class Person, and add it to the set as follows:
void ...
0
votes
0
answers
68
views
How can I add an element via emplace(pos, value) if I have a two-dimensional vector?
There is a code like this:
using d2dArray = vector<vector<double>>; // Псевдоним
d2dArray calcNextArray(d2dArray array)
{
d2dArray nextArray;
int nextArrSize = array.size() - 1;
...
0
votes
1
answer
65
views
Vector<pair>: emplace without constructing pair-inner objects
Suppose we have two structs:
struct FIRST{
int a=0;
int b=0;
FIRST(int a, int b): a(a), b(b){}
};
struct SECOND{
int c=0;
int d=0;
SECOND(int c, int d): c(c), d(d){}
};
And we ...
1
vote
2
answers
2k
views
How std::optional library handles emplace operation?
I was trying to create objects for optional variables in-place. When using emplace function in std optional library for a class, the destructor of the class is called. Does that function do copy ...
1
vote
3
answers
832
views
Why does the emplace method in std::unordered_map take multiple parameters?
This was a source of confusion for me, but std::unordered_map::try_emplace takes the key and variable arguments for the constructor of the value(mapped) type, however ::emplace is supposed to take an ...
0
votes
1
answer
125
views
Construction of an object inside a vector with emplace_back wipes previous value attribute data in map of corresponding object pointers
I am writing a function that creates a vector of Product objects from .csv file data and emplaces the object pointer value into a map with its corresponding Product ID as the key. When I run a test ...
3
votes
1
answer
1k
views
Aggregate initialization inside emplace_back
In the following code, I am trying to use aggregate initialization with emplace_back. The catch here is that emplace_back takes the constructor arguments and constructs the object directly in the ...
-1
votes
1
answer
93
views
Why emplacing back into vector does not work with copy semantics disabled via inheritance
I am trying to find out why in the following example, when I disable copy-semantics but preserve move-semantics, emplacing into std::vector works in one case but does not work in another (when ...
-3
votes
1
answer
282
views
vector emplace back, uniqure ptrs, raw ptrs and memory leak
I read one answer about using std::vector::emplace_back on std::vector<std::unique_ptr<SomeClass>> and getting memory leaks when passing raw pointers (my_vec.emplace_back(new SomeClass())) ...
2
votes
1
answer
818
views
emplace_back with initializer list
I'm trying to run the following code on VS2022:
#include <vector>
#include <initializer_list>
int main()
{
std::vector<std::vector<int>> vec;
//auto list = (std::...
6
votes
1
answer
1k
views
std::map try emplace vs emplace strange behaviour
The common advice is to prefer using std::map::try_emplace in preference to std::map::emplace in almost every instance.
I wrote a simple test to trace object creation/copying/moving/destruction when ...
1
vote
2
answers
2k
views
C++ vectors: emplace_back vs. push_back
Why is the method emplace_back() necessary at all?
Why can compiler constructors not simply use the faster "create at end" way, when implementing push_back()?
What part of the C++ standard ...
4
votes
1
answer
2k
views
Copy/move elision vs emplace with std::optional
I was using an std::optional to store a struct, and I initially went with:
std::optional<Point_t> optPosition; // declared somewhere as a class member
optPosition.emplace(..., ..., ...);
works ...
2
votes
1
answer
120
views
Emplacing to the back of a forward_list segfaults
This must be a stupid oversight on my part, but how can I add to the back of a std::forward_list? I know usually the forward_list implementation doesn't contain an iterator to the back, but according ...
1
vote
1
answer
227
views
What is happening in std::vector::emplace() implementation?
I implement my own vector class just to practice my C++ skill and my emplace() method is way slower than std::vector::emplace() so I checked the source code to see how they implemented it but it is ...
0
votes
0
answers
52
views
Is emplace always efficient than insert when adding an element into a map?
In my thought, emplace is always a better option than insert, because in some case emplace could save one time of ctor and in other cases it could directly replace insert with the same invoking form.
...
0
votes
1
answer
550
views
How to use try_emplace in a nested map to still meet the purpose what emplace achieve?
First I have codes like
typedef struct st_A{
int a = 0;
string s;
st_A(const int a, const string&s) :a(a),s(s) {}
}st_A;
map<string, st_A> m1;
Now I want to insert a new ...
2
votes
1
answer
515
views
How to make map.emplace automatically choose constructor in c++17?
In c++17 first, I have a struct like
typedef struct ST_A{
int a;
string s;
ST_A(const int a, const string&s) :a(a),s(s) {}
}st_A;
And Now I have a simple map and emplace twice in ...
0
votes
1
answer
260
views
C++: emplace to std::unordered_map value containing std::mutex
Could you, please, help me to figure out the right syntax of how to emplace into an std::unordered_map a value containing an std::mutex?
Here is an example:
#include <mutex>
#include <...
0
votes
1
answer
166
views
Force decay of string literals (const char array) to ptr
In the following example I try to emplace_back() a string literal and a string_view into an std::pair of string_views. However, my problem is that emplace_back() takes up the string literal as a const ...
2
votes
1
answer
230
views
nested vector with polymorphic_allocator cannot be constructed via emplace_back
The following code:
std::pmr::vector<std::pmr::vector<int>> outer_vec(std::pmr::get_default_resource());
outer_vec.emplace_back(std::pmr::get_default_resource());
fails with quite ...
0
votes
2
answers
119
views
Does gcc have a extension overload for std::vector::emplace_back?
According to cppreference, std::vector::emplace_back has only one signature, which is:
template< class... Args >
reference emplace_back( Args&&... args );
And in the description, it ...
1
vote
1
answer
112
views
Emplacing an instance with constant fields fails to compile (C++)
I have an example_class with two constant fields. When I try to emplace an object of that class into a std::vector, I get various errors such as "error: object of type 'example_class' cannot be ...
0
votes
1
answer
188
views
How do I insert an element to a set that is a value in multimap C++
I have a trouble with adding a new values to a set<Value_Class> that is in my map<Key, set<Values_Class>>. When I try to add a value like this:
Key key(get<0>(key_args), get<...
0
votes
1
answer
806
views
emplace and try_emplace with copy constructor
I have an issue with emplace and try_emplace as they always use the copy constructors when moving an object in.
#include <iostream>
#include <unordered_map>
#include <map>
using ...
0
votes
2
answers
188
views
Emplace method in own deque
How can I write my own emplace method in my deque?
For example, I have this emplace_front function
template <typename ... Args>
void emplace_front(Args&& ...val) {
???
}
and Node ...
2
votes
2
answers
498
views
How does std::map's emplace() avoid premature construction?
I am confused about std::maps's implementation of emplace(). emplace() is a variadic template function with the following declaration:
template <class Args...>
iterator emplace( Args&&......
3
votes
1
answer
1k
views
Why std::set does not provide try_emplace member function?
One advantage of try_emplace member function of std::map (and std::unordered_map) is that it does not allocate a new node if the key already exists in the map. I wonder why this member function has ...
0
votes
2
answers
82
views
Error in implementing emplace_back() binary '=': no operator found which takes a right-hand operand of type 'T *'
Code below, for my linked list implementation and specifically emplace_back below. What am I doing wrong and how to fix?
Error I am getting is:
Build started...
1>------ Build started: Project: ...
2
votes
2
answers
217
views
C++: insert element into std::map<MyStruct> where MyStruct can only be aggregate initialized and contains const unique pointers
Here is what I am trying to do and what I have tried:
#include <memory>
#include <map>
#include <string>
using namespace std;
struct MyStruct {
const unique_ptr<int> a;
...
1
vote
1
answer
237
views
Emplacing an std::pair of strings to an unordered map reusing the string's heap
I have an unordered map of string-pairs that i reference via a message id (key). Now I want to construct the string pair in place using the temporary string objects that the function receives, hence ...
0
votes
1
answer
144
views
c++17 correct moving resource between unique_ptr in containers
How to correct move resource from existed unique_ptr to another created in container?
I want to put to container some unique_ptr with resource from another unique_ptr. Or maybe move one unique_ptr ...
-1
votes
1
answer
2k
views
Problem understanding the behaviour of std::map try_emplace for a composite key
I have a std::map<CompositeKey, std::string>, where CompositeKey is a class I wrote.
This CompositeKey has three int data members, all the constructors, all the copy assignment operators and a ...
1
vote
1
answer
341
views
Emplace a std::array of non-movable objects that cannot be default constructed
I have a class that contains a std::mutex so it is not movable or copiable.
struct MyObject {
MyObject(std::string s_) : s(s_) {};
std::mutex lock;
std::thread worker;
std::string s;
};
I can ...
2
votes
1
answer
410
views
Pointer to const object with emplace_back
I don't understand when we can use const variables/objects in collections (particularly with emplace). The below code works with const objects directly but not when using pointers to const objects.
#...
1
vote
1
answer
217
views
Erasing a tuple from a set with tuple and custom sort function
How can we remove elements from a set like below which has iterator as part of its key? .erase() throws error saying no matching member function for call to 'erase'. Just like emplace() can be used ...
2
votes
0
answers
44
views
C++ vector emplace_back fail with piecewise_construct [duplicate]
I was trying to use C++ std::vector's emplace_back (along with std::piecewise_construct and std::forward_as_tuple) on a vector of tuple, but compiler complain about template argument deduction/...
0
votes
1
answer
53
views
Building a std::map and issue with using std::emplace
Code:
std::map<CString, S_DISCUSSION_HIST_ITEM> mapHistory;
// History list is in ascending date order
for (auto& sHistItem : m_listDiscussionItemHist)
{
if (m_bFullHistoryMode)
...
0
votes
3
answers
799
views
Unable to avoid copying while pushing objects with copy-construcor into a vector
I'm trying to avoid copying with emplace_back() and reserve(). But when I've tried to do it, i caught myself getting 3 copies for reason i cannot really understand. reserve() actually helps to avoid ...
0
votes
1
answer
1k
views
Is there ever a time where insert is better than emplace on a map in C++?
To head this off, I've already read through the following question. My question could be seen as a follow up to this. insert vs emplace vs operator[] in c++ map
From what info I can find, I don't see ...
2
votes
2
answers
217
views
Vector of Base unique_ptr causes object slicing on emplace_back(new T())
I'm trying to pass a type as a argument to a method that will properly construct and push a object to a vector of unique_ptr, however the created object is always the Base object. This happens when ...
0
votes
1
answer
155
views
Equivalent for emplace and reserve functions in C program
I am trying to convert the following piece of code from C++ to C.
I created an array, but I'm not sure what can be a precise replacement for emplace() and reserve() and for size_t, what can be done in ...