Thread: Can you incorporate smart pointers into the implementation of containers?

  1. #1
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665

    Can you incorporate smart pointers into the implementation of containers?

    Like, if you had a memory pool, could you somehow incorporate smart pointers into all of that? I feel like you can and should be able to but for some reason, I'm not sure.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For some containers, maybe. But at that level, I think the container often requires fine-grained control over the lifetime of the elements, so a smart pointer might just get in the way, whereas an allocator provides a better abstraction.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by MutantJohn View Post
    Like, if you had a memory pool, could you somehow incorporate smart pointers into all of that? I feel like you can and should be able to but for some reason, I'm not sure.
    It depends what you mean by incorporating. I'm only guessing that you mean something like std::set<std::shared_ptr<std::string>> which holds preallocated container values.

    I can point out that the main problem with allocators is that they must be type-agnostic, whereas smart pointers are type-specific. The fact that they are templates is very misleading - this is wrong, allocators are not truly generic. For example, when you pass std::allocator<T> to some container<T> (e.g.: list<T>), there is no guarantee that container will use that implementation of allocator to allocate elements of T directly. Allocators can be rebound to any other type U by the container. It means that every implementation of an allocator must actually be able to allocate all possible types. For example, to avoid memory fragmentation, a list<T> container instead of allocating T directly, may instead choose to allocate a wrapper structure (node) containing: value (of type T), pointer to previous node, and pointer to next node. You cannot handle that scenario in a set, because the wrapper (node) type depends on specific implementation and you have no information of how it looks like (it might even not exist at all).

    Also, std::vector is kind of a special container: it is required to store its elements in a contiguous chunk of memory, so using std::shared_ptrs for individual elements is out of question (it would no longer be a contiguous allocator).

    Quote Originally Posted by laserlight View Post
    For some containers, maybe. But at that level, I think the container often requires fine-grained control over the lifetime of the elements, so a smart pointer might just get in the way, whereas an allocator provides a better abstraction.
    This is true that allocators are mostly used to find uninitialized storage, however, allocators can optionally provide a construct() and destroy() member functions, so in theory you could make it no-operations and prevent construction and destruction of elements altogether (I doubt it would work due to other issues, though).
    Last edited by kmdv; 03-18-2016 at 02:40 AM.

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Huh, that was an interesting read, kmdv.

    But my sentiments were the same, laser. I'll keep thinking about it in the back of my mind though. Might someday become relevant.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suggestion Smart Pointer Class Implementation
    By Bargi in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2015, 06:57 AM
  2. When to use smart pointers?
    By Neo1 in forum C++ Programming
    Replies: 1
    Last Post: 04-15-2012, 10:53 AM
  3. Smart Accessor Function for Multiple Data Containers?
    By phummon in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2012, 01:50 PM
  4. Objects or pointers in STL containers?
    By zacs7 in forum C++ Programming
    Replies: 7
    Last Post: 06-23-2009, 10:25 AM
  5. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM