string_view and associative container

921 views
Skip to first unread message

Magnus Fromreide

unread,
Oct 7, 2014, 5:31:27 PM10/7/14
to std-pr...@isocpp.org
Hello.

I tried to use a string_view to look up elements in a map<string,X> and got
surprised because it failed.

Looking further into it revealed that map::find takes an argument of type
const key_type& and string_view isn't convertible to string (and that is
a good thing)

I then asked myself why map::find takes a key_type argument when it really
just needs to have something that is LessThanComparable with key_type and
uses the same partial ordering.

Would a change of

iterator map::find(const key_type&)

to

template <typename K>
iterator map::find(const K&);

be reasonable?

In the default case this would still allow only key_type's since
map::compare defaults to less<key_type> and that prevents all other types,
and so the new version is compatible with the old one.

On the other hand this would allow one to declare a map using less<void>
as comparator and then every type that is comparable to key_type would
be useable for lookup of elements, and I think this would make the
associative containers a lot more powerful.

Of course the change should be applied to all lookup functions in all
associative containers if it is done.

/MF

Zhihao Yuan

unread,
Oct 7, 2014, 5:46:49 PM10/7/14
to std-pr...@isocpp.org
On Tue, Oct 7, 2014 at 5:31 PM, Magnus Fromreide <ma...@lysator.liu.se> wrote:
Hello.

I tried to use a string_view to look up elements in a map<string,X> and got
surprised because it failed.

Looking further into it revealed that map::find takes an argument of type
const key_type& and string_view isn't convertible to string (and that is
a good thing)

I then asked myself why map::find takes a key_type argument when it really
just needs to have something that is LessThanComparable with key_type and
uses the same partial ordering.

Would a change of

iterator map::find(const key_type&)

to

template <typename K>
iterator map::find(const K&);

be reasonable?



--
Zhihao Yuan, ID lichray
The best way to predict the future is to invent it.
___________________________________________________
4BSD -- http://bit.ly/blog4bsd
Reply all
Reply to author
Forward
0 new messages