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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:12:18+00:00 2026-05-12T13:12:18+00:00

If I had a STL container, say a list of pointers I could remove

  • 0

If I had a STL container, say a list of pointers I could remove them like in the exmple below. With a container of weak_ptrs this does not work, because they cannot be compared, as they need to be locked first. What can I do?

void MyClass::RemoveItem(std::tr1::weak_ptr<Item> const & pItem)
{
    mylist.remove(pItem);
}
  • 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-12T13:12:19+00:00Added an answer on May 12, 2026 at 1:12 pm

    For one thing, you could just define operator == for any weak_ptr. I’m sure there’s a reason this is not implemented, it can probably bite you at a later point.

    template <typename T>
    bool operator == (const std::tr1::weak_ptr<T>& a, const std::tr1::weak_ptr<T>& b)
    {
        return a.lock() == b.lock();
    }
    

    … and you’ll be able to just call remove() as usual. This is a little bit extreme I guess.

    If you stick to the remove_if() approach, you could get rid of the bind magic* by using function object:

    struct EqPredicate
    {
        const boost::weak_ptr<Item>& theItem;
    
        EqPredicate(const boost::weak_ptr<Item>& item) : theItem(item) 
        {
        }
    
        bool operator () (const boost::weak_ptr<Item>& p) const 
        { 
            return p.lock() == theItem.lock(); 
        }
    };
    

    and then use it like this:

    mylist.remove_if(EqPredicate(pItem));
    

    It looks like more code, but you can compress the EqPredicate class, it’s mostly hollow. Also, it could be made template to use it with lists containing types other than Item.

    Oh, and do pass you weak_ptrs by reference everywhere including your comparison function.

    *bind is not free performance-wise. If you expect a lot of Remove() calls and care much about performance it might be good to avoid it.

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

Sidebar

Related Questions

Is there any performance advantage to be had when using template parameters with static
For my homework project, I had to read in a book and then parse
I'm working with the Android NDK, and since it does not currently support the
So this is possibly the strangest thing I've seen recently and was curious how
Had a look over SO but I can't see any threads which address my
I spent a considerable amount of time coding in Baeza-Yates' fast set intersection algorithm
I am trying to compile something that uses Google's sparsehash include files. libs/include/google/dense_hash_map:93:60: error:
I'm looking for a C++ libraries that can work with Visual Studio 2008/2010, but
I haven't been programming for very long and am new to C/C++. I've always
EDIT : I added __iter__ to my Map class (i forgot it didn't inherit

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.