我正在写一些包装std :: map的容器类。简化版本是否为:
#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);
}
这段代码可以在gcc和clang上正常编译,但是在Visual C ++上(我测试了2015版以及http://rextester.com/l/cpp_online_compiler_visual上使用的任何版本),它会失败并显示:
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
因此,Visual C ++以某种方式假定value
map2 :: update()的参数类型std::pair<key_type, value_type>
而不是value_type
。但是为什么在gcc和clang接受我的代码很好的情况下这样做呢?
问题可能出在Visual C ++没有做的事情上,而这是两阶段的名称查找。由于名称查找只有一个阶段,因此必须value_type
在该阶段进行查找。而且作为一种技巧,VC ++已经开始研究依赖的基类,例如std::map<key_type, value_type>
此处。
这会错误地找到std::map<K, V>::value_type
的typedef pair<K,V>
。这说明了VC ++错误消息。
解决方法:消除map2
自己中的参数名称的歧义。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句