Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boilerplate typedefs for STL-compatible container

Is there, within the standard library or Boost, some kind of utility base class for populating a custom STL-compatible Sequence with the required typedefs (size_type, value_type, etc...). I'm thinking of something like boost::iterator_facade, but for containers.

I was going to roll-up my own, but wanted to make sure such a thing didn't already exist.

UPDATE:

This is the utility base class I came up with, in case anybody finds it useful:

template <class C>
class ContainerAdapter
{
public:
    typedef C::value_type value_type;
    typedef C::reference reference;
    typedef C::const_reference const_reference;
    typedef C::const_iterator iterator;
    typedef C::const_iterator const_iterator;
    typedef C::difference_type difference_type;
    typedef C::size_type size_type;

protected:
    typedef C::container_type;
};


// Usage
class MyCustomContainer : public ContainerAdapter< std::vector<int> >
{
...
};

ContainerAdapter simply "echoes" the nested typedefs of a custom container's underlying container. There's nothing to it, really.

like image 323
Emile Cormier Avatar asked Jan 27 '11 22:01

Emile Cormier


People also ask

How are STL containers implemented?

They are implemented as class templates, which allows great flexibility in the types supported as elements. The container manages the storage space for its elements and provides member functions to access them, either directly or through iterators (reference objects with similar properties to pointers).

Are STL containers allocated on heap?

std::vector always has its buffer allocated on heap. So regardless of where the vector itself is allocated resizing it will only affect the heap.


1 Answers

even if it does exist, you still have to typedef typename base::size_type size_type. does not seem you would gain much.

like image 73
Anycorn Avatar answered Sep 19 '22 19:09

Anycorn