我认为以下简化的 C++11 代码应该是有效的。
unordered_map<string,string> test;
auto it = remove_if( test.begin(), test.end(),
[] (const decltype(test)::value_type &entry) { return true; } );
但它无法使用 g++ 6.3 编译,提示 std::pair 的赋值运算符已删除,但 AFAIK 未删除该运算符。
/usr/include/c++/6/bits/stl_algo.h:868:16: error: use of deleted function ‘std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=( ...
*__result = _GLIBCXX_MOVE(*__first);
这是编译器/glibc 错误还是由于某些原因我看不到代码真的无效?
最佳答案
我们来看remove_if文档:
The type of dereferenced ForwardIt must meet the requirements of MoveAssignable.
也就是说,“给定
t
,类型为 T
的可修改左值表达式和 rv
,类型为 T
的右值表达式, 表达式 t = rv
必须有效并且[行为如预期]”。在这里,您通过
unordered_map<string, string>
的迭代器到 remove_if
.我们来看一下。根据 unordered_map
documentation ,
value_type
is defined asstd::pair<const Key, T>
所以,
std::pair<const string, string>
.让我们看看对的
operator=
.最为显着地:
template< class U1, class U2 > pair& operator=( const pair<U1,U2>& other );
does not participate in overload resolution unlessstd::is_assignable_v<first_type&, const U1&>
andstd::is_assignable_v<second_type&, const U2&>
are both true.
在这里,
std::is_assignable_v<const string&, const string&>
不是真的,因此运算符不可用。因此,该对不是 MoveAssignable。因此 remove_if
不能用于那些迭代器。所以这会使您的代码无效。
关于c++11 - 对<string,string>的删除赋值运算符的g++编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46421095/