emplace

performance of emplace is worse than check followed by emplace

非 Y 不嫁゛ 提交于 2019-12-04 18:11:32
问题 I have a std::unordered_map with a value_type that does not have a default constructor so I cannot do the following auto k = get_key(); auto& v = my_map[k]; I ended up writing a helper function value_type& get_value(key_type& key) { return std::get<0>(my_map.emplace( std::piecewise_construct, std::forward_as_tuple(key), std::forward_as_tuple(args_to_construct_value) ))->second; } but the performance was markedly worse (i.e. the value_type's constructor showed up in perf) than the following

Why can an aggreggate struct be brace-initialized, but not emplaced using the same list of arguments as in the brace initialization?

徘徊边缘 提交于 2019-12-03 12:48:54
It seems like this code : #include <string> #include <vector> struct bla { std::string a; int b; }; int main() { std::vector<bla> v; v.emplace_back("string", 42); } could be made to work properly in this case, but it doesn't (and I understand why). Giving bla a constructor solves this, but removes the aggregateness of the type, which can have far-reaching consequences. Is this an oversight in the Standard? Or am I missing certain cases where this will blow up in my face, or is it just not as useful as I think? Is this an oversight in the Standard? It is considered an open defect in the

performance of emplace is worse than check followed by emplace

独自空忆成欢 提交于 2019-12-03 12:08:29
I have a std::unordered_map with a value_type that does not have a default constructor so I cannot do the following auto k = get_key(); auto& v = my_map[k]; I ended up writing a helper function value_type& get_value(key_type& key) { return std::get<0>(my_map.emplace( std::piecewise_construct, std::forward_as_tuple(key), std::forward_as_tuple(args_to_construct_value) ))->second; } but the performance was markedly worse (i.e. the value_type's constructor showed up in perf) than the following version. value_type& get_value(key_type& key) { auto it = my_map.find(key); if (it == my_map.end())

Efficiency of C++11 push_back() with std::move versus emplace_back() for already constructed objects

蓝咒 提交于 2019-12-03 01:59:44
问题 In C++11 emplace_back() is generally preferred (in terms of efficiency) to push_back() as it allows in-place construction, but is this still the case when using push_back(std::move()) with an already-constructed object? For instance, is emplace_back() still preferred in cases like the following? std::string mystring("hello world"); std::vector<std::string> myvector; myvector.emplace_back(mystring); myvector.push_back(std::move(mystring)); // (of course assuming we don't care about using the

Is it safe to use emplace_back with a container of unique_ptrs?

空扰寡人 提交于 2019-11-29 03:05:29
Consider the following: std::vector<std::unique_ptr<int>> ptrsToInts; ptrsToInts.emplace_back(new int); If reallocation occurs in the vector, and that fails (throwing std::bad_alloc ), am I "safe" or will I leak an int ? C++11 23.3.6.5 [vector.modifiers]/1 says: If an exception is thrown other than by the copy constructor, move constructor, assignment operator, or move assignment operator of T or by any InputIterator operation there are no effects. which seems to indicate that this is a potential problem. That is, if there are "no effects", then no unique_ptr ever was constructed, and

Insert into vector having objects without copy constructor

你说的曾经没有我的故事 提交于 2019-11-28 19:36:38
I have a class whose copy constructors are explicitly deleted (because A uses pointers internally and I don't want to fall into shallow copy pitfalls): class A { public: A(const A&) = delete; A& operator=(const A&) = delete; A(const B& b, const C& c); } Now I have a vector of type vector<A> aVector; and I want to insert elements into it - so I use emplace_back : aVector.emplace_back(b, c); However, this fails to compile using gcc and I get the error - third-party/gcc-4.7.1-glibc-2.14.1/libgcc/libgcc-4.7.1/afc21dc/include/c++/4.7.1/bits/stl_construct.h: In instantiation of 'void std::_Construct

Why emplace_back is faster than push_back?

醉酒当歌 提交于 2019-11-27 17:31:22
I thought that emplace_back would be the winner, when doing something like this: v.push_back(myClass(arg1, arg2)); because emplace_back would construct the object immediately in the vector, while push_back , would first construct an anonymous object and then would copy it to the vector. For more see this question. Google also gives this and this questions. I decided to compare them for a vector that would be filled by integers. Here is the experiment code: #include <iostream> #include <vector> #include <ctime> #include <ratio> #include <chrono> using namespace std; using namespace std::chrono;

Is it safe to use emplace_back with a container of unique_ptrs?

让人想犯罪 __ 提交于 2019-11-27 17:22:16
问题 Consider the following: std::vector<std::unique_ptr<int>> ptrsToInts; ptrsToInts.emplace_back(new int); If reallocation occurs in the vector, and that fails (throwing std::bad_alloc ), am I "safe" or will I leak an int ? C++11 23.3.6.5 [vector.modifiers]/1 says: If an exception is thrown other than by the copy constructor, move constructor, assignment operator, or move assignment operator of T or by any InputIterator operation there are no effects. which seems to indicate that this is a

Insert into vector having objects without copy constructor

♀尐吖头ヾ 提交于 2019-11-27 12:22:16
问题 I have a class whose copy constructors are explicitly deleted (because A uses pointers internally and I don't want to fall into shallow copy pitfalls): class A { public: A(const A&) = delete; A& operator=(const A&) = delete; A(const B& b, const C& c); } Now I have a vector of type vector<A> aVector; and I want to insert elements into it - so I use emplace_back : aVector.emplace_back(b, c); However, this fails to compile using gcc and I get the error - third-party/gcc-4.7.1-glibc-2.14.1/libgcc

why do i need to use piecewise_construct in map::emplace for single arg constructors of noncopyable objects?

倖福魔咒の 提交于 2019-11-27 01:32:49
问题 The following code will not compile on gcc 4.8.2. The problem is that this code will attempt to copy construct an std::pair<int, A> which can't happen due to struct A missing copy and move constructors. Is gcc failing here or am I missing something? #include <map> struct A { int bla; A(int blub):bla(blub){} A(A&&) = delete; A(const A&) = delete; A& operator=(A&&) = delete; A& operator=(const A&) = delete; }; int main() { std::map<int, A> map; map.emplace(1, 2); // doesn't work map.emplace(std