Search code examples
c++c++11stlconstantsassignment-operator

C++ default copy/move assignment operator for extern C structure not const


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


Solution

  • 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.