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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:11:38+00:00 2026-05-27T09:11:38+00:00

Because of my noob reputation, I cannot reply to this Thread , in specific

  • 0

Because of my noob reputation, I cannot reply to this Thread, in specific the accepted answer:

I never used boost::intrusive smart pointers, but if you would use shared_ptr smart pointers, you could use weak_ptr objects for your cache.

Those weak_ptr pointers do not count as a reference when the system decides to free their memory, but can be used to retrieve a shared_ptr as long as the object has not been deleted yet.

This is certainly an intuitive idea, however, the C++ standard does not support comparison of weak_ptrs, so it cannot be used as key for associative containers. This could be circumvented by implementing a comparison operator for weak_ptrs:

template<class Ty1, class Ty2>
    bool operator<(
        const weak_ptr<Ty1>& _Left,
        const weak_ptr<Ty2>& _Right
    );

The problem with this solution is that

(1) the comparison operator has to obtain ownership for every comparison (i.e. creating shared_ptrs from the weak_ptr refs)

(2) the weak_ptr is not erased from the cache when the last shared_ptr that manages the resource is destroyed, but an expired weak_ptr is kept in the cache.

For (2), we could provide a custom destructor (DeleteThread), however, this would require again to create a weak_ptr from the T* that is to delete, which can then be used to erase the weak_ptr from the cache.

My question would be if there is any better approach to a cache using smart pointers (I am using the VC100 compiler, no boost), or do I simply not get it?

Cheers, Daniel

  • 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-27T09:11:39+00:00Added an answer on May 27, 2026 at 9:11 am

    The thing is, your Cache is not addressed by the object cached itself, otherwise it would be about useless.

    The idea of a Cache is to avoid some computation, so the index will be the parameters of the computation, which will directly map to the result if already present.

    Now, you might actually need a second index, to remove objects from the cache, but it is not mandatory. There are certainly other strategies available.

    If you really want to remove objects from the cache as soon as they are not used anywhere else in the application, then, effectively, you could use a secondary index. The idea here though will be to index according to T*, not weak_ptr<T>, but to keep weak_ptr<T> around, because otherwise you cannot create a new shared_ptr on the same reference count.

    The exact structure depends on whether the parameters of the computation are hard to recompute after the fact, if they are, a simple solution is:

    template <typename K, typename V>
    class Cache: boost::enable_shared_from_this<Cache>
    {
      typedef std::map<K, boost::weak_ptr<V>> KeyValueMap;
      typedef std::map<V*, KeyValueMap::iterator> DeleterMap;
    
      struct Deleter {
        Deleter(boost::weak_ptr<Cache> c): _cache(c) {}
    
        void operator()(V* v) {
          boost::shared_ptr<Cache> cache = _cache.lock();
          if (cache.get() == 0) { delete v; return; }
    
          DeleterMap::iterator it = _cache.delmap.find(v);
          _cache.key2val.erase(it->second);
          _delmap.erase(it);
          delete v;
        }
    
        boost::weak_ptr<Cache> _cache;
      }; // Deleter
    
    public:
      size_t size() const { return _key2val.size(); }
    
      boost::shared_ptr<V> get(K const& k) const {
        KeyValueMap::const_iterator it = _key2val.find(k);
        if (it != _key2val.end()) { return boost::shared_ptr<V>(it->second); }
    
        // need to create it
        boost::shared_ptr<V> ptr(new_value(k),
            Deleter(boost::shared_from_this()));
    
        KeyValueMap::iterator kv = _key2val.insert(std::make_pair(k, ptr)).first;
        _delmap.insert(std::make_pair(ptr.get(), kv));
    
        return ptr;
      }
    
    
    private:
      mutable KeyValueMap _key2val;
      mutable DeleterMap _delmap;
    };
    

    Note the special difficult: the pointer might outlive the Cache, so we need some trick here…

    And for your information, while it seems feasible, I am not at all confident in this code: untested, unproven, bla, bla 😉

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

Sidebar

Related Questions

Sorry because this is a noob question. I'm new with MySQL: I wrote a
This is a noob question. So if I misstate the question, it's because I
MySQL noob here; looked around first but couldn't find the answer to this question.
This is a bit noob question but.. Its happens sometimes to everybody. I never
Because this is my first attempt at an extension method that seems quite useful
Forgive me for this noob question, but is there such a setting that sets
I'm a real noob to encryption. After breaking my head over this for a
I realise this is an incredibly noob question, but I've googled for it and
Hey i am a noob when it comes to php because i have just
Noob question (because I'm a noob when it comes to Web development). I'm not

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.