I am trying to summarise to some colleagues how std::find
works, and I wanted to show them how tricky can be to use it on a std::map
(and why they shouldn't), so I started fiddling on compiler explorer.
I think I run into a implementation discrepancy between libc++
and libstdc++
, since the following snippet compiles on the former
#include <string>
#include <map>
int main (){
std::map<std::string, int> myMap;
myMap["string1"] = 100;
std::map<std::string, int>::value_type element("string1", 100);
auto it = std::find(myMap.begin(), myMap.end(), element);
}
but fails to compile with the latter generating the following error
error: no matching function for call to 'find'
auto it = std::find(myMap.begin(), myMap.end(), element);
^~~~~~~~~
/opt/compiler-explorer/gcc-9.2.0/lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0/bits/streambuf_iterator.h:373:5: note: candidate template ignored: could not match 'istreambuf_iterator' against '_Rb_tree_iterator'
find(istreambuf_iterator<_CharT> __first,
^
1 error generated.
So I am puzzled and I wonder which of the two is the desired behaviour. Compiler Explorer links:
- with libc++ https://godbolt.org/z/KDsMsC
- with libstdc++ https://godbolt.org/z/g3DqlJ
Aucun commentaire:
Enregistrer un commentaire