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 7621749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:16:21+00:00 2026-05-31T04:16:21+00:00

Problem I have a template container MyContainer<std::unique_ptr<Foo>> which has a std::deque<T> and a std::vector<T>

  • 0

Problem

I have a template container MyContainer<std::unique_ptr<Foo>> which has a std::deque<T> and a std::vector<T> member.

Inside method, send_to_purgatory_if( predicate ), I would like to look at all items in m_taskdq and move items from m_taskdq to m_purgatory, if the predicate evaluates to true.

Issues

I have two issues that I’m struggling with:

  • my iterator it gets trashed if I remove items from m_taskdq from inside the loop
  • I am worried about the state of the std::unique_ptr<> if I do the move in two steps (problem lines 1 and 2 – by line 2, I think the std::unique_ptr<> pointed to by it is undefined?)

How should I fix this code?

    template <typename T>
    class MyContainer
    {
      typedef std::function<bool(T&)>  PREDICATE;
    
      void send_to_purgatory_if( PREDICATE p )
      {
// bad code -------------------------------------
        for( auto it=m_taskdq.begin(); it!=m_taskdq.end(); ++it )
        {
          if ( p( *it ) )
          {
            m_purgatory.emplace_back( move( *it ));  // problem line 1
            m_taskdq.erase( it );                    // problem line 2
          }
        }
// end bad code ---------------------------------
      }
    
      std::deque<  T >  m_taskdq;                                                     
      std::vector< T >  m_purgatory;
    };
  • 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-31T04:16:22+00:00Added an answer on May 31, 2026 at 4:16 am

    This is really a C++98 question, with a red-herring concerning move semantics. The first thing to ask is how to do this in C++98:

    std::deque::erase(iterator) returns an iterator that refers to the element after the one erased. So get that working first:

     void send_to_purgatory_if( PREDICATE p )
      {
        for( auto it=m_taskdq.begin(); it!=m_taskdq.end();)
        {
          if ( p( *it ) )
          {
            m_purgatory.emplace_back(*it);
            it = m_taskdq.erase(it);
          }
          else
            ++it;
        }
      }
    

    And now it is easy to make it work with C++11 move semantics:

     void send_to_purgatory_if( PREDICATE p )
      {
        for( auto it=m_taskdq.begin(); it!=m_taskdq.end();)
        {
          if ( p( *it ) )
          {
            m_purgatory.emplace_back(std::move(*it));
            it = m_taskdq.erase(it);
          }
          else
            ++it;
        }
      }
    

    The unique_ptr moved from in taskdq becomes a null unique_ptr after the emplace_back, and then it gets erased in the next line. No harm, no foul.

    When there is an erase, the return from the erase does a good job at incrementing the iterator. And when there is no erase, a normal iterator increment is in order.

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

Sidebar

Related Questions

The problem: I have a class which contains a template method execute which calls
I have the following problem using template instantiation [*]. file foo.h class Foo {
Problem: I have an address field from an Access database which has been converted
Background: I have template stream operators (e.g. operator << (ostream &, std::vector <T>) )
I have a problem with a template and pointers ( I think ). Below
I have a problem moving a Word template from Word 2003 to Word 2007
I have a problem in calling a template class I have. I declared a
i have bought a template that have built in contact form problem is that
I have a small problem with Smarty... I have two different template files in
I have the following simple button with an image as the template. The problem

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.