Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 years ago
Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
Is it possible the value passed to insert is left in a moved from state after move insertion if insertreturns false?
insert
false
#include #include <map> #include struct less { template< typename T > bool operator () (const std::shared_ptr<T> & lhs, const std::shared_ptr<T> & rhs) const { return *lhs < *rhs; } }; int main() { using key_type = int; using value_type = int; using map_type = std::map<std::shared_ptr, std::shared_ptr, less>; map_type m; auto p = typename map_type::value_type{std::make_shared(1), std::make_shared(1)}; if (!m.insert(p).second) { assert(false); } assert(p.first); assert(p.second); if (m.insert(std::move(p)).second) { assert(false); } assert(p.first); assert(p.second); }
Is the behavior of the last two assertion implementation defined?
From [map.modifiers/2] on std::map::insert, we have
std::map::insert
template<class P> pair<iterator, bool> insert(P&& x); [...] Effects: The first form is equivalent to return emplace(std::forward(x)).
template<class P> pair<iterator, bool> insert(P&& x);
[...]
Effects: The first form is equivalent to return emplace(std::forward(x)).
return emplace(std::forward(x))
(x))
So it's in std::map::emplace... from [associative.reqmts/8] (emphasis mine):
std::map::emplace
a_uniq.emplace(args) Effects: Inserts a value_type object t constructed with std::forwardrgs>(args)... if and only if there is no element in the container with key equivalent to the key of t.
a_uniq.emplace(args)
Effects: Inserts a value_type object t constructed with std::forwardrgs>(args)... if and only if there is no element in the container with key equivalent to the key of t.
value_type
t
std::forwardrgs>(args)...
Hence, construction does not take place if there is already an object in the container that is associated with an equivalent key.
Let's verify with from the Llvm implementation. In what follows, I've deleted some parts of the code to make it more readable. First, std::map::insert does this:
template <class _Pp, /* some SFINAE... */> /* symbol visibility ... */ pair<iterator, bool> insert(_Pp&& __p) {return __tree_.__insert_unique(_VSTD::forward<_Pp>(__p));}
Let's go to __tree::insert_unique, then:
__tree::insert_unique
pair<iterator, bool> __insert_unique(__container_value_type&& __v) { return __emplace_unique_key_args(_NodeTypes::__get_key(__v), _VSTD::move(__v)); }
Still not there... but in __tree::emplace_unique_key_args it comes:
__tree::emplace_unique_key_args
/* Template, template, template... return value... template */ __tree</* ... */>::__emplace_unique_key_args(_Key const& __k, _Args& __args) { __parent_pointer __parent; __node_base_pointer& __child = __find_equal(__parent, __k); __node_pointer __r = static_cast<__node_pointer>(__child); bool __inserted = false; if (__child == nullptr) { /* Some legacy dispatch for C++03... */ // THIS IS IT: __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get())); __r = __h.release(); __inserted = true; } return pair< REPLY 0 views 0 likes 0 shares Facebook Twitter Linked In WhatsApp
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.