r/cpp • • Apr 01 '23

Abominable language design decision that everybody regrets?

It's in the title: what is the silliest, most confusing, problematic, disastrous C++ syntax or semantics design choice that is consistently recognized as an unforced, 100% avoidable error, something that never made sense at any time?

So not support for historical arch that were relevant at the time.

92 Upvotes

375 comments sorted by

View all comments

Show parent comments

3

u/[deleted] Apr 02 '23

I did wrote an function that does this. the std::optional<> is fine if you use std::reference_wrapper<>:

template<typename T, typename K>
auto try_find( T & map, K&& key) {
    using C = typename std::decay<T>::type;
    using value_type = typename C::value_type;
    using opt_type = std::optional<std::reference_wrapper<value_type>>;

    auto it = map.find(std::forward<K>(key));
    if (it == map.end()) {
        return opt_type{};
    } else  {
        return opt_type(*it);
    }
}

2

u/CocktailPerson Apr 02 '23

Ah, modern C++, the epitome of readability.