I'm writing some container class that wraps std::map. A simplified version if it is:
#include <map>
template <typename key_type, typename value_type>
class map2 : private std::map<key_type, value_type>
{
public:
void update(const key_type& key, value_type value)
{
(*this)[key] = std::move(value);
}
};
int main()
{
map2<int, int> m;
m.update(1,4);
}
This code compiles just fine on gcc and clang, but on Visual C++ (I tested the 2015 version and whatever is used on http://rextester.com/l/cpp_online_compiler_visual ) it fails with:
source_file.cpp(16): error C2664: 'void map2<int,int>::update(const int &,std::pair<const _Kty,_Ty>)': cannot convert argument 2 from 'int' to 'std::pair<const _Kty,_Ty>'
with
[
_Kty=int,
_Ty=int
]
source_file.cpp(16): note: No constructor could take the source type, or constructor overload resolution was ambiguous
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64
So Visual C++ somehow assumes that the value
parameter of map2::update() is of type std::pair<key_type, value_type>
and not of value_type
. But why is it doing that, while gcc and clang accept my code just fine?