dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: A confusing struct (used with std::set)

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,496

    A confusing struct (used with std::set)

    I program I'm working on defines a type called controllables. It's a std::set which holds pointers to objects of type Controllable:-

    Code:
    typedef std::set <boost::shared_ptr<Controllable>> Controllables;
    Controllables controllables;
    so far, so good... A bit further on there's a function to remove an entry from the set :-

    Code:
    struct null_deleter { void operator()(void const *) const {} };
    
    void
    Session::remove_controllable (Controllable* c)
    {
    	Controllables::iterator x = controllables.find (boost::shared_ptr<Controllable>(c, null_deleter()));
    
    	if (x != controllables.end()) {
    		controllables.erase (x);
    	}
    }
    I'm just curious to know what's the purpose of null_deleter() here? If I remove the references to it, the code still compiles but it crashes at the controllables.find() line
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    Join Date
    Feb 2017
    Posts
    361

    Re: A confusing struct (used with std::set)

    Quote Originally Posted by John E View Post
    I'm just curious to know what's the purpose of null_deleter() here? If I remove the references to it, the code still compiles but it crashes at the controllables.find() line
    The c pointer is turned into a boost::shared_pointer because the find() method expects a boost::shared_pointer as parameter. After the call to find() the boost::shared_pointer would normally delete c but since null_deleter() is specified it gets called instead and since it does nothing c stands undeleted.

    So null_deleter() saves the day. It prevents c from being deleted when passed as a boost::shared_ptr parameter to find().

    In my view, if a smart pointer type is introduced the underlying raw pointer should not be used on its own. It spells trouble.
    Last edited by wolle; November 7th, 2018 at 03:56 AM.

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,496

    Re: A confusing struct (used with std::set)

    Great explanation, wolle - and many thanks.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  4. #4
    Join Date
    Feb 2017
    Posts
    361

    Re: A confusing struct (used with std::set)

    Quote Originally Posted by John E View Post
    Great explanation, wolle - and many thanks.
    You're welcome. If you don't mind here are a few comments on the code.

    Firstly, an std::set is used. This means the smart pointers will be held in sorted order but if the application doesn't rely on that an std::unordered_set may be used instead. The complexity of accesses will drop from O(log N) to O(1) and that means faster code for large sets.

    Secondly, raw pointers are stored as smart pointers in the std:set. If raw pointers are used throughout and the sole purpose of using smart pointers in the set is to gain automatic deletion, then an std::map/std::unordered_map may be used instead. Each raw pointer and associated smart pointer will be held as a pair in the map (raw pointer as key type and smart pointer (preferably an std::unique_ptr) as mapped type). Accesses to the map is by raw pointer and when a raw pointer entry is erased from the map its object gets deleted courtesy of the smart pointer companion. This is in accordance with the principle of separation of concerns and the result is cleaner code. The obscure null_deleter "trick" is no longer necessary. On the downside, since each pointer now is stored twice, both as raw and as smart, the storage requirements have doubled, so it is a trade-off.
    Last edited by wolle; November 10th, 2018 at 01:46 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)