简体   繁体   English

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

[英]Class derived from std::map does not compile on Visual C++ (but compiles on gcc and clang)

I'm writing some container class that wraps std::map. 我正在写一些包装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: 这段代码可以在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

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 . 因此,Visual C ++以某种方式假定map2 :: update()的value参数类型为std::pair<key_type, value_type>而不是value_type But why is it doing that, while gcc and clang accept my code just fine? 但是为什么在gcc和clang接受我的代码很好的情况下这样做呢?

The problem is likely what Visual C++ is not doing, and that's two-phase name lookup. 问题可能出在Visual C ++ 没有做的事情上,而这是两阶段的名称查找。 Since there's only one phase of name lookup, it will have to look up value_type in that phase. 由于只有一个阶段的名称查找,因此必须在该阶段查找value_type And as a hack, VC++ then already looks into dependent base classes such as std::map<key_type, value_type> here. 而且,作为一种技巧,VC ++现在已经在这里查找依赖的基类,例如std::map<key_type, value_type>

This incorrectly finds std::map<K, V>::value_type , a typedef for pair<K,V> . 这会错误地找到std::map<K, V>::value_typepair<K,V> This explains the VC++ error message. 这说明了VC ++错误消息。

Workaround: Disambiguate the argument name in map2 yourself. 解决方法:自己消除map2的参数名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有自动参数的 Function 使用 GCC 编译,但不使用 Visual C++ 编译 - Function with auto parameter compiles using GCC but does not compile using Visual C++ 为什么下面的代码不能用gcc编译,但用clang编译好 - Why the below code does not compile with gcc but compiles fine with clang Visual C ++初始化与gcc和clang的不一致 - Visual C++ initialization inconsistence with gcc and clang 自定义操纵器使用Visual C ++编译,但不使用g ++ / clang编译 - Custom manipulator compiles with Visual C++ but not g++/clang 来自派生类的C ++ std :: function绑定 - C++ std::function bind from derived class 从源代码构建的Clang编译C而不是C ++代码 - Clang built from source compiles C but not C++ code C++ std::vector 构造中的不同行为 gcc 和 clang - Different behaviour in C++ std::vector construction with gcc and clang 为什么在使用clang -std = gnu ++ 11时编译这个C ++代码? - Why does this C++ code compile when using clang -std=gnu++11? 在std :: vector &lt;std :: string&gt;上找到std :: find在Visual C ++ 2008中不能编译? - std::find on std::vector< std::string > does not compile in Visual C++ 2008? c ++编译问题—使用cl.exe可以正常编译,但无法从Visual Studio 2015进行编译 - c++ compiling issue — Compiles fine with cl.exe but will not compile from Visual Studio 2015
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM