Alex Rivera | Logout

Reducing template bloat with inheritance

Asked 2010-06-14T13:07:16.767
10

Does anyone have experience reducing template code bloat through inheritance?

I hesitate rewriting our containers this way:

class vectorBase
{
  public:
    int size();
    void clear();
    int m_size;
    void *m_rawData;
    //....
};

template< typename T > 
class vector : public vectorBase
{
    void push_back( const T& );
    //...

};

I should keep maximum performance while reducing compile time. I'm also wondering why Standard Library implementations do not uses this approach.

Edit
Report

1 Answer

1

Some implementations do use (a form of) the above approach. Here is GCC's

  template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
    class vector : protected _Vector_base<_Tp, _Alloc>
    {
    ... 
    }

In this case, the goal is to delegate memory management to _Vector_base. If you do choose to spend your time reinventing STL, please follow up here with your results. Perhaps your efforts will help put an end to the old "code bloat" cries still heard from time to time.

answered 2010-06-15T01:46:29.827

Your Answer