7

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.

Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
  • People use value_type and iterator because it's easier than alternatives, but I commonly use value_type& instead of reference, for example (and vector is evil). Are you sure providing those other nested typedefs is advantageous for you? Normally I pass items to functions where they'll be bound as a const& anyway, and that nested const_reference typedef is never touched. – Fred Nurk Jan 27 '11 at 23:34
  • @Fred: I'm just worried about my container being compatible with things such as BOOST_FOREACH, boost ranges, std::back_insertion_iterator, etc. If I only need to provide a minimal subset of all the typedefs, I'd like to know which. I suppose it's easy enough to experiment and find out. – Emile Cormier Jan 28 '11 at 00:32
  • True, but general utilities are usually written to be agnostic; 0x auto is great for that. :) – Fred Nurk Jan 28 '11 at 00:36
  • This question made me spawn another related one: http://stackoverflow.com/questions/4823761 – Emile Cormier Jan 28 '11 at 01:08
  • 1
    Related question: http://stackoverflow.com/questions/1533917/c-template-macro-shortcut I think the best solution might be to have a struct tucked away, like in the answer to that question, then have a series of using declarations done with a macro. – GManNickG Jan 28 '11 at 03:33

1 Answers1

1

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

Anycorn
  • 50,217
  • 42
  • 167
  • 261
  • You only need to re-typedef in the derived class if you want the typedefs to be visible within the class declaration. Users of the custom container would still see all typedefs inherited from the base class. This is how `boost::iterator_facade` works. – Emile Cormier Jan 27 '11 at 22:31
  • @Emilie I know, but assuming for containers you have to provide operators and other things which will use those types. – Anycorn Jan 27 '11 at 22:33
  • @aaa: I can always use `T`, `T&`, `const T&`, etc. The only typedefs I really need (within the declaration) are `iterator` and `const_iterator`. However, I admit that having `reference`, `value_type`, etc in the member function declarations is prettier from an documentation point-of-view. – Emile Cormier Jan 27 '11 at 22:39
  • I've tried rolling my own, and things just got... messy. You were right that I didn't gain much. – Emile Cormier Jan 27 '11 at 23:16