【问题标题】:Filling std::map with std::transform (error: cannot deduce argument)用 std::transform 填充 std::map (错误:无法推断参数)
【发布时间】:2011-08-20 17:53:54
【问题描述】:

我正在尝试用 std::transform 填充 std::map。接下来的代码编译没有错误:

std::set<std::wstring> in; // "in" is filled with data
std::map<std::wstring, unsigned> out;

std::transform(in.begin(), in.end()
,   boost::counting_iterator<unsigned>(0)
,   std::inserter(out, out.end())
,   [] (std::wstring _str, unsigned _val) { return std::make_pair(_str, _val); }
);

但是如果我替换字符串

,   [] (std::wstring _str, unsigned _val) { return std::make_pair(_str, _val); }

,   std::make_pair<std::wstring, unsigned>

,   std::ptr_fun(std::make_pair<std::wstring, unsigned>)

我收到错误:

foo.cpp(327): error C2784: '_OutTy *std::transform(_InIt1,_InIt1,_InTy (&)[_InSize],_OutTy (&)[_OutSize],_Fn2)' : could not deduce template argument for '_InTy (&)[_InSize]' from 'boost::counting_iterator<Incrementable>'
      with
      [
          Incrementable=unsigned int
      ]
      C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\algorithm(1293) : see declaration of 'std::transform'
foo.cpp(327): error C2784: '_OutTy *std::transform(_InIt1,_InIt1,_InIt2,_OutTy (&)[_OutSize],_Fn2)' : could not deduce template argument for '_OutTy (&)[_OutSize]' from 'std::insert_iterator<_Container>'
      with
      [
          _Container=std::map<std::wstring,unsigned int>
      ]
      C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\algorithm(1279) : see declaration of 'std::transform'
foo.cpp(327): error C2784: '_OutIt std::transform(_InIt1,_InIt1,_InTy (&)[_InSize],_OutIt,_Fn2)' : could not deduce template argument for '_InTy (&)[_InSize]' from 'boost::counting_iterator<Incrementable>'
      with
      [
          Incrementable=unsigned int
      ]
      C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\algorithm(1267) : see declaration of 'std::transform'
foo.cpp(327): error C2914: 'std::transform' : cannot deduce template argument as function argument is ambiguous

等等…… 请解释编译的问题是什么?

更新:感谢您的回答。我意识到,这是 MSVC2010 错误。顺便说一句

&std::make_pair<const std::wstring&, const unsigned&>

导致同样的错误

【问题讨论】:

标签: c++ visual-c++ stl c++11 visual-c++-2010


【解决方案1】:

g++ 4.4.5 编译它没有错误。好像是Visual Studio 10 defect

【讨论】:

    【解决方案2】:

    这是 Visual C++ 库实现中的一个错误。下面的程序演示了这个问题:

    #include <utility>
    
    int main()
    {
        &std::make_pair<int, int>;
    };
    

    这个程序产生错误:

    error C2568: 'identifier' : unable to resolve function overload
    

    在 C++ 语言规范中,make_pair 不是重载函数。 Visual C++ 2010 库实现包括四个重载,采用左值和右值引用的各种组合。

    虽然允许 C++ 标准库的实现为成员函数添加重载,但不允许为非成员函数添加重载,因此这是一个错误。

    错误was reported and will be fixed in the next version of Visual C++。但是,正如 STL 在解决该错误时指出的那样,您需要将模板参数设置为 std::make_pair 左值引用:

    &std::make_pair<const std::wstring&, const unsigned&>
    

    【讨论】:

      猜你喜欢
      • 2010-12-03
      • 2017-12-23
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      • 1970-01-01
      • 2018-04-07
      相关资源
      最近更新 更多