从std :: map派生的类不能在Visual C ++上编译(但可以在gcc和clang上编译)

梦想家

我正在写一些包装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 ++以某种方式假定valuemap2 :: 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

C++:std::visit 不能在 gcc 下编译

类模板的构造函数声明不能为 C++20 编译,但可以为 C++17 编译

ldd:找不到用于xl c编译二进制文件的库,但可以在AIX上找到gcc编译二进制文件的库

GCC和Clang无法在C ++ 17中编译std :: hash <std :: nullptr_t>

为什么这个C ++程序可以在MacOS上编译而不能在Ubuntu上编译?

std::vector 的 std:array 的 SFINAE 不能在 C++11 中编译

类模板继承无法在GCC中编译,但可以在Visual Studio中使用

C ++ std :: map在“ *”上失败?

Java:不能对匿名内部类使用“<>”,不能在 IDE 中运行,但可以在终端中编译和运行

gcc编译选项std = c ++ 1y和std = c ++ 14之间的区别

在Ubuntu 14.04上编译EOS时缺少C ++ std库方法和其他错误?

错误C3699:'^':不能在类型'std :: string'上使用此间接寻址

Eclipse内容辅助无法识别std :: thread,但可以正确编译

C ++程序可以在代码块中编译和运行,但不能在终端中编译

为什么`++ a ++`不能在C ++中编译而`(++ a)++`可以编译?

使用std :: map和自定义类作为其值的编译器错误C2664

C ++ std :: function从派生类绑定

在新版本的gcc上返回隐式不可复制结构的std :: map时出现编译错误

AppIcon不能在带有xamarin / Visual Studio Mac的IOS上显示,但可以与xcode一起使用

在C ++中定义和使用std :: map时发生编译错误

如何使用自动工具使用clang和选项-std = c ++ 11编译项目

为什么%运算符不能在C / C ++中用于浮点运算,但可以在Java和C#中使用?

std :: vector :: push_back()不能在MSVC上针对已删除move构造函数的对象进行编译

std :: map非常量函子,在clang ++下编译时错误,对于g ++来说可以

std :: to_chars编译但未在MacOS / clang上链接

Gitlab 不能在服务器上克隆,但可以在客户端克隆

按钮上的填充不能在本地工作,但可以在JSFiddle上工作

C ++ std :: thread和方法类

C++ std::random 和枚举类