1

I have a std::map<CXCursor, DeclarationContent> from which I want to remove elements using std::remove_if.

CXCursor is a (typedef of a) struct in external C code (libClang) that I cannot / must not modify.

Error messages from clang++ 3.4.2 --std=c++11 :

stl_pair.h:170:8: error: no viable overloaded '='
stl_algo.h:1152:23: note: in instantiation of member function std::pair<const CXCursor, DeclarationContent>::operator=' requested here

extract.cc:34:8: in instantiation of function template specialization 'std::remove_if<std::_Rb_tree_iterator<std::pair<const CXCursor, DeclarationContent> >, bool (*)(std::pair<const CXCursor, DeclarationContent> &)>' requested here
  std::remove_if(decls.begin(), decls.end(), noCodeGenerationRequested);

include/clang-c/Index.h:2137:9: note: candidate function (the implicit copy assignment operator) not viable:
  'this' argument has type 'const CXCursor', but method is not marked const

/include/clang-c/Index.h:2137:9: note: candidate function (the implicit move assignment operator) not viable:
  'this' argument has type 'const CXCursor', but method is not marked const

The code is basically:

std::map<CXCursor, DeclarationContent> decls;
// filling the map
std::remove_if(decls.begin(), decls.end(), noCodeGenerationRequested);

with

bool noCodeGenerationRequested(std::map<CXCursor, DeclarationContent>::value_type & v)
{ /* FUN GOES HERE */ return true; }

From reading the error message it seems that the implicit assignment operators are not const qualified, which is needed in case of a map (as it's key is always const).

I could write a wrapper class around CXCursor that provides those assignment operators, but maybe there is another way?


std::remove_if does not work with a std::map, see: remove_if equivalent for std::map

Community
  • 1
  • 1
Daniel Jour
  • 15,896
  • 2
  • 36
  • 63

1 Answers1

2

map is not usable with std::remove_if, since map::value_type has type std::pair<const T, U>, but std::remove_if requires, that dereferenced iterator should be MoveAssignable. Use just loop, or probably copy_if in another container with negation of your predicate.

Or you can use boost::range::remove, if you already use boost in your project.

ForEveR
  • 55,233
  • 2
  • 119
  • 133
  • Oh .. must have missed that when reading remove_if docs. Thanks a lot, you saved me from writing a wrapper that wouldn't have helped anything. For reference, I think I'll be going with erase_if which is presented here: http://stackoverflow.com/a/16597048/1116364 – Daniel Jour Feb 26 '15 at 08:39