c++ - "error C2678: binary ' = ' : no operator found which takes a left-hand operand of type..."使用lambda函数进行 map 过滤时

标签 c++ c++11

我写了(部分复制:))下面的代码: 高阶函数.h :

#ifndef HigherOrderFunctions_H
#define HigherOrderFunctions_H

#include <algorithm>

class HigherOrderFunctions {
public:
   template<class Coll, class Func>
   static Coll& filter(Coll& items, Func&& filterFunc);
private:
   HigherOrderFunctions() {}
};

template < class Coll, class Func>
Coll& HigherOrderFunctions::filter(Coll& items, Func&& filterFunc) {
   auto first = std::find_if_not(items.begin(), items.end(), filterFunc);
   const auto last = items.end();
   if (first != last) {
      for(auto it = first; ++it != last; ) {
         if (filterFunc(*it)) {
            *first++ = std::move(*it);
         }
      }
   }
   items.erase(first, items.end());
   return items;
}

#endif // HigherOrderFunctions_H

现在当我尝试用测试函数测试上面的代码时:

void HighOrderFunctionsTest::testFilter() {
   vector<int> integers = {1, 2, 3, 4, 5, 6, 7};
   HigherOrderFunctions::filter(integers, [](int elem) -> bool {
      return (elem % 2) == 0;
   });

   const int ADULT_AGE = 18;
   map<string, int> name_age_list = {{"sm", 10}, {"ak", 20}, {"al", 30}, {"am", 35}};
   function<bool(const pair<string, int>&)> funct = [ADULT_AGE](const pair<string, int>& elem) -> bool {
      return (elem.second > ADULT_AGE);
   };

   HigherOrderFunctions::filter(name_age_list, funct);
}

我过滤 vector 的部分编译正常,但是当我添加带有 map 的部分时,我得到以下编译错误(使用 MSVS 2013):

HighOrderFunctionsTest.cpp
2>C:\winapp\MSVS2013PRO\VC\include\utility(175): error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion)
2>          C:\winapp\MSVS2013PRO\VC\include\xstring(1017): could be 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> &std::basic_string<char,std::char_traits<char>,std::allocator<char>>::operator =(_Elem)'
2>          with
2>          [
2>              _Elem=char
2>          ]
2>          C:\winapp\MSVS2013PRO\VC\include\xstring(1012): or       'std::basic_string<char,std::char_traits<char>,std::allocator<char>> &std::basic_string<char,std::char_traits<char>,std::allocator<char>>::operator =(const _Elem *)'
2>          with
2>          [
2>              _Elem=char
2>          ]
2>          C:\winapp\MSVS2013PRO\VC\include\xstring(996): or       'std::basic_string<char,std::char_traits<char>,std::allocator<char>> &std::basic_string<char,std::char_traits<char>,std::allocator<char>>::operator =(const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)'
2>          C:\winapp\MSVS2013PRO\VC\include\xstring(957): or       'std::basic_string<char,std::char_traits<char>,std::allocator<char>> &std::basic_string<char,std::char_traits<char>,std::allocator<char>>::operator =(std::initializer_list<_Elem>)'
2>          with
2>          [
2>              _Elem=char
2>          ]
2>          C:\winapp\MSVS2013PRO\VC\include\xstring(901): or       'std::basic_string<char,std::char_traits<char>,std::allocator<char>> &std::basic_string<char,std::char_traits<char>,std::allocator<char>>::operator =(std::basic_string<char,std::char_traits<char>,std::allocator<char>> &&) throw()'
2>          while trying to match the argument list '(const std::string, const std::string)'
2>          C:\winapp\MSVS2013PRO\VC\include\utility(174) : while compiling class template member function 'std::pair<const _Kty,_Ty> &std::pair<const _Kty,_Ty>::operator =(std::pair<const _Kty,_Ty> &&)'
2>          with
2>          [
2>              _Kty=std::string
2>  ,            _Ty=int
2>          ]
2>          D:\tax\tax_win\tc\src\cpp\foundation\HigherOrderFunctions.h(219) : see reference to function template instantiation 'std::pair<const _Kty,_Ty> &std::pair<const _Kty,_Ty>::operator =(std::pair<const _Kty,_Ty> &&)' being compiled
2>          with
2>          [
2>              _Kty=std::string
2>  ,            _Ty=int
2>          ]
2>          ..\..\..\..\..\..\src\cpp\foundation\test\HighOrderFunctionsTest.cpp(33) : see reference to class template instantiation 'std::pair<const _Kty,_Ty>' being compiled
2>          with
2>          [
2>              _Kty=std::string
2>  ,            _Ty=int
2>          ]

我查找了类似的错误,发现如果调用代码的函数是 const,则可能会发生这种情况,但事实并非如此。你能帮我解决这个问题吗?

最佳答案

问题是由 *first++ = std::move(*it); 引起的线。当容器是 map那么存储的项目将是 pair<const Key, Value> 类型.取消引用迭代器将产生对此类类型的引用,任何对其执行移动赋值的尝试都将失败,因为 Key 的值 |无法更改。

我认为这段代码的关键问题是过滤器函数应该生成一个具有过滤值的新容器,而不是改变提供的容器。当前实现的算法似乎与 std::remove_if 类似.

关于c++ - "error C2678: binary ' = ' : no operator found which takes a left-hand operand of type..."使用lambda函数进行 map 过滤时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48905923/

相关文章:

具有模板成员的 C++ 类,它使用任意模板参数构造模板成员

c++ - 在 C++ 中读取字节

c++ - 提供Predicate参数时为什么不能传递 “std”前缀

c++ - 多个 vector 元素的无重复组合

c++ - 执行 C++ 程序并使用 Perl 复制 cmd 输出

c++ - 函数签名的错误类型推导

c++ - 运算符 [ ] (std::vector) 不匹配

C++ 串行通信问题

c++ - 第一个 c++ 可能出现的语法错误

c++ - void 指针可以指向 lambda 函数吗?