Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unordered_set::remove_if(): C3892: cannot assign to a variable that is const

Tags:

c++

I have code like:

unordered_set<AttrValue> output;
...

auto requiredType = variables.at(arg.value);
auto end = remove_if(output.begin(), output.end(), 
    [&](AttrValue x) {
        return !matchingOutputType(requiredType, ast->getNodeType(ast->getNodeKeyAttribute(x)));
    }); // queryevaluator_getcandidatelist.cpp(179)
output.erase(end);

Error is on line 4 of the code. So I think its because of remove_if. But whats wrong? output is not defined constant?


Error   90  error C3892: '_Next' : you cannot assign to a variable that is const    c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    1840
Error   109 error C3892: '_Next' : you cannot assign to a variable that is const    c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    1840

Output window:

3>c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(1840): error C3892: '_Next' : you cannot assign to a variable that is const
3>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(1853) : see reference to function template instantiation '_FwdIt std::_Remove_if<std::_List_unchecked_const_iterator<_Mylist>,_Pr>(_FwdIt,_FwdIt,_Pr)' being compiled
3>          with
3>          [
3>              _FwdIt=std::_List_unchecked_const_iterator<std::_List_val<int,std::allocator<AttrValue>>>,
3>              _Mylist=std::_List_val<int,std::allocator<AttrValue>>,
3>              _Pr=`anonymous-namespace'::<lambda4>
3>          ]
3>          h:\dropbox\sch\cs3202\code\source\query\query evaluator\queryevaluator_getcandidatelist.cpp(179) : see reference to function template instantiation '_FwdIt std::remove_if<std::_List_const_iterator<_Mylist>,`anonymous-namespace'::<lambda4>>(_FwdIt,_FwdIt,_Pr)' being compiled
3>          with
3>          [
3>              _FwdIt=std::_List_const_iterator<std::_List_val<int,std::allocator<AttrValue>>>,
3>              _Mylist=std::_List_val<int,std::allocator<AttrValue>>,
3>              _Pr=`anonymous-namespace'::<lambda4>
3>          ]
like image 671
Jiew Meng Avatar asked Mar 30 '13 08:03

Jiew Meng


1 Answers

According to the standard § 23.2.4.6

For associative containers where the value type is the same as the key type, both iterator and const_iterator are constant iterators.

So, you can't even do

std::unordered_set<int> s{1, 2, 3, 4};
*s.begin() = 42;

And, of course, you can't use std::remove_if(ForwardIt first, ForwardIt last, ...) function for removing elements from std::set and std::unordered_set:

The type of dereferenced ForwardIt must meet the requirements of MoveAssignable.

like image 156
awesoon Avatar answered Oct 16 '22 04:10

awesoon