Welcome to mess.cpp - the file that describes some weird moments and caveats about C++. Some of them can lead to a disaster. Some of them will make yo

mess.cpp - specific things that will brighten up your life

submited by
Style Pass
2023-03-16 10:30:14

Welcome to mess.cpp - the file that describes some weird moments and caveats about C++. Some of them can lead to a disaster. Some of them will make you smile. But as you will notice at the end of this page, my main goal here is to raise a discussion about improving ecosystems instead of arguing about language features. Even if you skip most of the sections below, please read the Plea.

Easy, right? We iterate over a map and log the number. We also use a const reference to each element to prevent unnecessary copying.

We "iterate" over the value_type values, but the compiler sees that we want const std::pair<std::string, int>& instead.

What can a compiler do in this case? It somehow needs to convert std::pair<const std::string, int> to std::pair<std::string, int>, const the std::pair and give us a reference.

How to convert something const to something mutable? Yep, copy! the left pair, create a temporary one, AND because we asked for a reference, the compiler will actually give us a reference to the temporary object.

Leave a Comment