logoalt Hacker News

1718627440last Monday at 3:28 PM2 repliesview on HN

And what does casting const change, that would involve runtime inefficiencies?


Replies

gpderettalast Monday at 3:43 PM

It 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.

show 1 reply
nemetroidlast Monday at 3:58 PM

Each 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.