Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6922291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:23:14+00:00 2026-05-27T10:23:14+00:00

Given an std::vector<std::unique_ptr<SomeType> > , is it legal to use remove_if on it? In

  • 0

Given an std::vector<std::unique_ptr<SomeType> >, is it legal to use
remove_if on it? In other words, given this code:

std::vector<std::unique_ptr<SomeType> > v;
//  fill v, all entries point to a valid instance of SomeType...
v.erase( std::remove_if( v.begin(), v.end(), someCondition ), v.end() );

, am I guaranteed after the erase that all pointers still in v are
valid. I know that given the intuitive implementation of
std::remove_if, and given all of the implementations I’ve looked at,
they will be. I’d like to know if there is anything in the standard
which guarantees it; i.e. that std::remove_if is not allowed to copy
any of the valid entries without recopying the copy into its final
location.

(I am, of course, supposing that the condition doesn’t copy. If the
condition has a signature like:

struct Condition
{
    bool operator()( std::unique_ptr<SomeType> ptr ) const;
};

, then of course, all of the pointers will be invalid after
remove_if.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:23:15+00:00Added an answer on May 27, 2026 at 10:23 am

    25.3.8 in the N3290 speaks about remove function :

    Requires: The type of *first shall satisfy the MoveAssignable
    requirements (Table 22).

    and

    Note: each element in the range [ret,last), where ret is the returned
    value, has a valid but unspecified state, because the algorithms can
    eliminate elements by swapping with or moving from elements that were
    originally in that range.

    This means that it depends on your predicate operator. Since your predicate doesn’t create a copy, then the elements are not going to be copied.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given std::vector<CMyClass> objects; CMyClass list[MAX_OBJECT_COUNT]; Is it wise to do this? for(unsigned int i
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
I'd like to use a std::vector to control a given piece of memory. First
So I have a std::vector<std::unique_ptr<Base>> vec and I'm trying to sort it dynamically, given
std::next_permutation (and std::prev_permutation) permute all values in the range [first, last) given for a
It is easy given a container to get the associated iterators, example: std::vector<double>::iterator i;
Given: template<typename T> class A { B b; std::vector<T> vec1; std::vector<T> vec2; } I'd
A different question inspired the following thought: Does std::vector<T> have to move all the
Given allocators a1 and a2 , where a1 != a2 , and std::vector s
Given my_type m; std::vector<my_type> v; Which runs more quickly? m.generate_data_inside_self(); v.push_back(m); Or v.push_back(m); v[0].generate_data_inside_self();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.