C++ Logo

std-proposals

Advanced search

Re: Yet another member function for std::map

From: Olaf van der Spek <ml_at_[hidden]>
Date: Sun, 1 Aug 2021 11:19:14 +0200
On Wed, Jul 28, 2021 at 9:16 PM Kyle Knoepfel via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> Hi Barry,
>
> Right...which is why I said the return type of value_for(...) would not be quite like std::optional. So there seem to be four or five options to me:
>
> 1. std::map<K, V>::value_for(K const&) -> typename std::map<K, V>::value_handle, where map gains a nested type that behaves like std::optional<V&> would behave. Yuck.
> 2. std::map<K, V>::value_for(K const&) -> V*, where a bare pointer, or some equivalent, is returned. Also yuck.

I've been using functions like #2 (sometimes you want something else
than a pointer to second) for years and it works great.
Trying to use something like optional only makes everything more complex IMO.

const typename T::value_type::second_type* find_ptr(const T& c, const
U& v) // returns pointer to second

typename T::value_type::second_type* find_ptr(T& c, const U& v)

const typename T::value_type::second_type& find_ptr2(const T& c, const
U& v) // returns reference to second or to a (static) default value

typename T::value_type::second_type& find_ptr2(T& c, const U& v)

const typename T::value_type* find_ptr0(const T& c, const U& v) //
returns pointer to pair

typename T::value_type::second_type& find_ref(T& c, const U& v)

const typename T::value_type::second_type& find_ref(const T& c, const
U& v) // returns reference to second, requires key to be present (a
bit like at, but doesn't throw)

const typename T::value_type::second_type& find_ref(const T& c, const
U& v, const typename T::value_type::second_type& z) // returns
reference to second or to a provided default value

typename T::value_type::second_type& find_ref(T& c, const U& v,
typename T::value_type::second_type& z)

https://github.com/OlafvdSpek/xbt/blob/master/misc/xbt/find_ptr.h

Received on 2021-08-01 04:19:28