The std::remove and std::remove_if compact a range so that the leading sub-range [begin, last) does not contain any elements that match the provided value or for which the predicate evaluates to true.

The number of elements in the range is unaffected, the non-removed elements maintain their relative order and the subrange [last, end) contains elements in a moved-from state.

Compiler Explorer link: compiler-explorer.com/z/65zoKv

#cpp #cplusplus #coding #programming #dailybiteofcpp

@simontoth
"the subrange [last, end) contains elements in a moved-from state."

can you point out where that property is guaranteed? I doubt

(cannot try on my phone, but you can check with a vector of unique_ptr and

auto it=remove_if(begin(v),end(v), [](auto const &p){return p!= nullptr;});

assert(all_of(it,end(v),[](auto const &p){return p==nullptr;}));

Follow

@PeterSommerlad @simontoth

From cppreference:

**
Relative order of the elements that remain is preserved and the physical size of the container is unchanged. Iterators pointing to an element between the new logical end and the physical end of the range are still dereferenceable, but the elements themselves have unspecified values (as per MoveAssignable post-condition) (since C++11).
**

Doesn't the last sentence say the state of the [last, end) is invalid, i.e., unusable? That they are in a "moved-from state"?

@rmerriam @simontoth

logically you have to consider the being 'moved from', because you only reasonably can destroy them or re-assign. technically, they might still have their original value.

moved-from state is overloaded, it can mean

a) you don't know what state an object has after an operation: 'valid but unspecified'
b) you are talking about a manager type object (such as unique_ptr or vector), where the state after being moved from is empty.

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.