"Conversion loses qualifiers" error on wrapper for std::erase_if function

6 days ago 14
ARTICLE AD BOX

I'm writing a convenience wrapper for the std::erase_if function which takes a vector reference vec and a const object used for checking value equality and then removing. This has to be the function signature. The object should play no role in removal, so it shouldn't matter to the lambda if it's const or not.

The following implementation

template <typename T> inline void vector_removeIfEquals(const std::vector<T>& vec, const T& object) { std::erase_if(vec, [object](T t) { return t == object; }); }

produces the error Conversion loses qualifiers on compilation, with the additional line See declaration of std::erase_if.

And so do these versions:

template <typename T> void vector_removeIfEquals(const std::vector<T>& vec, const T& object) { std::erase_if(vec, [&](const T& t) { return t == object; }); } template <typename T> void vector_removeIfEquals(const std::vector<T>& vec, const T& object) { // Create a non-const copy? T objectToRemove(object); std::erase_if(vec, [objectToRemove](T t) { return t == objectToRemove; }); }

Any tips?

gene b.'s user avatar

3

Read Entire Article