In my code, I have something like this:
unordered_multimap<string, unordered_map<string, string> > mEntities; ... vector<unordered_map<string, string> > rawEntities; if (qi::phrase_parse(&buf[0], (&buf[0]) + buf.size(), EntityParser<char*>(), qi::space, rawEntities)) { for (auto &propGroup : rawEntities) { auto search = propGroup.find("classname"); if (search != propGroup.end()) { // is stealing propGroup safe??? mEntities.emplace(search->second, std::move(propGroup)); } } } // rawEntities goes out of scope here
As you can see, I use std::move for an object of type displayed on unordered_map<string, string>& , which obviously is not unordered_map<string, string>&& . However, I know for sure that since rawEntities goes beyond the scope after the for loop, its elements (strings-> string charts) will never be used again. Therefore, I believe it is safe to steal (move) the data of my elements because they will no longer be used.
When I run the program, it works. But is this bad practice / anti-pattern, and in particular, the standard warranty is safe?
c ++ c ++ 11 move lvalue
bombax
source share