
On 18 Jul 2025 13:31, Ivan Matek via Boost wrote:
On Sat, Dec 21, 2024 at 2:33 PM Peter Dimov via Boost <boost@lists.boost.org> wrote:
Andrey Semashev wrote:
On 12/21/24 15:11, Peter Dimov via Boost wrote:
Christian Mazakas wrote: What this user has always wanted is to not have to write this:
auto it = map.find( key ); if( it != map.end() ) { // do something with it->second }
That's why I'm always defining a helper function `lookup` that returns a pointer, which allows me to write this instead:
if( auto p = lookup( map, key ) ) { // do something with *p }
I'll note that with C++17 you can also write this:
if ( auto it = map.find( key ); it != map.end() ) { // do something with it->second }
But each of the three variants seem pretty much equivalent to me, from the code clarity standpoint.
It was admittedly much worse in C++03, where you had to spell out
std::map<key_type, mapped_type>::const_iterator it
in its entirety. Plus a typename in front if you happened to be in a template.
C++17 makes it almost palatable, except for the need to repeat `it->second`.
I know I am resurrecting ancient thread, but I just noticed there is a proposal for this, but it will not be in C++26
*P3091R4 was forwarded to LWG for C++29 (with request to align wording with format in associative containers as described)* https://github.com/cplusplus/papers/issues/1759 https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p3091r4.html#before...
In any case I still think it would be great to see this in Boost, even though I am not fan of get name.
Are people who disliked this fine with this change if we only do it when compiler/stdlib has std::optional<T&>, and we do not depend on boost::optional<T&>?
I'd rather not introduce the dependency, neither on boost::optional nor on std::optional. I don't find the "After" syntax compelling, especially where it replaces an `if` with a `for`. To me, such a change would detriment code clarity. If you still want this, make it a standalone utility in a separate header and not part of the core container interface.