Boost logo

Boost :

From: Daniel James (daniel_at_[hidden])
Date: 2005-02-25 11:16:14


christopher diggins wrote:
> When inheriting from an STL container the constructors and local
> typedefs need to be redeclared. For instance the vector contract class
> at http://www.ootl.org/pwc/pwc_vector.hpp.htm .

Only if you want to use them inside the class without qualification.

> It seems to me that it would be very useful to have a single macro for
> each of the different STL containers to ease inheritance of them. Does
> Boost already have something like that? If not would anyone else want
> something like this for Boost? Or am I really missing something basic
> here, and this is a bad idea for some obscure C++ reason?

I think publicly inheriting from a class that isn't designed for
inheritance is a bad idea. There are type safety problems, non-virtual
destructors, slicing etc.

It's generally better to use composition, or perhaps protected or
private inheritance with 'using' statements to make the appropriate
functions public.

> i.e.
>
> #define BOOST_STL_VECTOR_OVERRIDE(NAME, ELEMENT) /* */ \
> typedef std::vector<ELEMENT> inherited; \
> typedef typename inherited::size_type size_type; \
> typedef typename inherited::reference reference; \
[snip]

I don't know how portable this is, but if you're using single
inheritance, you might be able to write:

#define BOOST_STL_VECTOR_OVERRIDE(NAME) /* */ \
     typedef typename NAME::value_type value_type; \
     typedef typename NAME::allocator_type; \
     typedef std::vector<value_type, allocator_type> inherited;

Daniel


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk