Visual Studio 2013地图初始化

时间:2017-11-21 23:29:52

标签: c++ visual-studio-2013 assignment-operator stdmap

使用Visual Studio 2013时,我遇到了使用initializer_list初始化地图的问题。

以下工作正常:

#include <iostream>
#include <map>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
    map<string, string> m = { { "key_1", "value_1" }, { "key_2", "value_2" } };

    for (auto kv : m)
    {
        cout << "[" << kv.first << ", " << kv.second << "]" << endl;
    }

    return 0;
}

然而,以下失败:

#include <iostream>
#include <map>
#include <string>

using namespace std;

class Foo {
public:
    // ctor
    Foo() {}
    // getter
    const map<string, string>& fooMap() const { return fooMap_; }
    // setter
    map<string, string>& fooMap() { return fooMap_; }
private:
    map<string, string> fooMap_;
};

int main(int argc, char* argv[])
{
    Foo foo;

    // Following two lines work
    // map<string, string> m = { { "key_1", "value_1" }, { "key_2", "value_2" } };
    // foo.fooMap() = m;

    // This assignment doesn't work
    foo.fooMap() = { { "key_1", "value_1" }, { "key_2", "value_2" } };

    for (auto kv : foo.fooMap())
    {
        cout << "[" << kv.first << ", " << kv.second << "]" << endl;
    }

    return 0;
}

出现此错误:

1>------ Build started: Project: map_test, Configuration: Release x64 ------
1>  map_test.cpp
1>map_test.cpp(22): error C2593: 'operator =' is ambiguous
1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\map(212): could be 'std::map<std::string,std::string,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>> &std::map<_Kty,_Ty,std::less<_Ty>,std::allocator<std::pair<const _Kty,_Ty>>>::operator =(std::initializer_list<std::pair<const _Kty,_Ty>>)'
1>          with
1>          [
1>              _Kty=std::string
1>  ,            _Ty=std::string
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\map(166): or       'std::map<std::string,std::string,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>> &std::map<_Kty,_Ty,std::less<_Ty>,std::allocator<std::pair<const _Kty,_Ty>>>::operator =(std::map<_Kty,_Ty,std::less<_Ty>,std::allocator<std::pair<const _Kty,_Ty>>> &&)'
1>          with
1>          [
1>              _Kty=std::string
1>  ,            _Ty=std::string
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\map(150): or       'std::map<std::string,std::string,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>> &std::map<_Kty,_Ty,std::less<_Ty>,std::allocator<std::pair<const _Kty,_Ty>>>::operator =(const std::map<_Kty,_Ty,std::less<_Ty>,std::allocator<std::pair<const _Kty,_Ty>>> &)'
1>          with
1>          [
1>              _Kty=std::string
1>  ,            _Ty=std::string
1>          ]
1>          while trying to match the argument list '(std::map<std::string,std::string,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>, initializer-list)'
1>          with
1>          [
1>              _Kty=std::string
1>  ,            _Ty=std::string
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

这是Visual Studio 2013编译器的问题(我使用的是版本12.0.40629.00 Update 5)吗?它对我来说看起来像编译器问题,但我想仔细检查以防我错过了什么。

修改 实际上,相同失败的一个更简单的例子是:

#include <iostream>
#include <map>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
    map<string, string> m;
    // This does not work
    m = { { "key_1", "value_1" }, { "key_2", "value_2" } };

    for (auto kv : m)
    {
        cout << "[" << kv.first << ", " << kv.second << "]" << endl;
    }

    return 0;
}

0 个答案:

没有答案