7 ms·
Why is this a perf footgun? As someone who doesn't write a lot of c++, I don't see anything intuitively wrong. Is it that iterating over map yields something o
by dubi_steinkek 9mo ago
Why is this a perf footgun? As someone who doesn't write a lot of c++, I don't see anything intuitively wrong.
Is it that iterating over map yields something other than `std::pair`, but which can be converted to `std::pair` (with nontrivial cost) and that result is bound by reference?
- nemetroid 9mo agoClose, it is a std::pair, but it differs in constness. Iterating a std::map<K, V> yields std::pair<const K, V>, so you have: std::pair<const std::string, int> vs std::pair<std::string, int>
- 1718627440 9mo agoAnd what does casting const change, that would involve runtime inefficiencies?
- gpderetta 9mo agoIt is not a cast. std::pair<const std::string, ...> and std::pair<std::string,...> are different types, although there is an implicit conversion. So a temporary is implicitly created and bound to the const reference. So not only there is a copy, you have a reference to an object that is destroyed at end of scope when you might expect it to live further.
- 1718627440 9mo agoI guess this is one of the reasons, why I don't use C++. Temporaries is a topic, where C++ on one side and me and C on the other side has had disagreements in the past. Why does changing the type even create another object at all? Why does it allocate? Why doesn't the optimizer use the effective type to optimize that away?
- jcelerier 9mo ago> Why does changing the type even create another object at all? There's no such thing as "changing the type" in c++. Function returns an object type A, your variable is of type B, compiler tries to see if there is a conversion of the value of type A to a new value of type B
- nemetroid 9mo agoEach entry in the map will be copied. In C++, const T& is allowed to bind to a temporary object (whose lifetime will be extended). So a new pair is implicitly constructed, and the reference binds to this object.