-1

I'm looking for a way to create a base container class that inherits the functionality of the boost multi index container. I want to be able to add other functions to this base class and create other classes that can use this base class's functions and utilize boost's multi index container.

I tried something like:

template < class D, E >
class BoostModelContainer : public boost::multi_index_container<D, E>
{
public:
    D* AddItem( const D& item)
    {
        //code here
    }
};

and then created other classes inheriting the base class like:

class ExampleContainer : public BoostModelContainer< CItem,
boost::multi_index::indexed_by<
  boost::multi_index::ordered_unique< 
     boost::multi_index::tag<id_tag>, boost::multi_index::member< CItem, ItemId, &CItem::m_id > >,
  boost::multi_index::ordered_unique< 
     boost::multi_index::tag<name_tag>, boost::multi_index::member< CItem, String, &CItem::m_name > >
  >
>

But this wouldn't compile. Does anyone have any other ideas or know how to get this to work?

Thank you!!

  • 1
    Don't know enough of your specific case, but I'll drop this here anyway: [Prefer composition over inheritance?](http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance) – user4581301 Oct 12 '16 at 19:14
  • 1
    I don't think composition would work? I am trying to create many various containers with different multi index key structures. And I want all of the containers to have access to base functions, rather than putting the functions in each of the containers. And I want each container to be a boost multi index container. – Trevor Davis Oct 12 '16 at 19:27
  • 1
    That's just a "Consider your alternatives" comment. There isn't enough in the question to make a good call one way or the other. Sometimes inheritance from a container is appropriate, but usually it isn't. – user4581301 Oct 12 '16 at 20:38

2 Answers2

2

You're passing two template arguments but your template only takes one.

template < class D, class E >
class BoostModelContainer : public boost::multi_index_container<D, E>
{
public:
    D* AddItem( const D& item)
    {
        //code here
    }
};
2

It's a bit hard to figure out exactly what your problem is without a minimal, complete and verifiable example. But rightfold's answer definitely points out the main problem with the code you posted. Essentially you are missing a class keyword in your template declaration:

template < class D, class E >
// missing ---------^^^^^
class BoostModelContainer : public boost::multi_index_container<D, E>
{ /* ... */ };

Apart from that, it should work just fine, as the following complete code example shows (compiles fine, see live demo):

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <string>

template <class D, class E>
class BoostModelContainer : public boost::multi_index_container<D, E>
{
public:
  D* AddItem( const D& item)
  {
    //code here
  }
};

struct Foo
{
  int id;
  std::string name;
};

struct id_tag { };
struct name_tag { };

// Requires -std=c++11    
using ExampleContainer = BoostModelContainer<
  Foo,
  boost::multi_index::indexed_by<
    boost::multi_index::ordered_unique<
      boost::multi_index::tag<id_tag>, boost::multi_index::member<Foo, int, &Foo::id> >,
    boost::multi_index::ordered_unique<
      boost::multi_index::tag<name_tag>, boost::multi_index::member<Foo, std::string, &Foo::name> >
    >
  >;

// works with c++03 as well
struct ExampleContainer2 : public BoostModelContainer<
  Foo,
  boost::multi_index::indexed_by<
    boost::multi_index::ordered_unique<
      boost::multi_index::tag<id_tag>, boost::multi_index::member<Foo, int, &Foo::id> >,
    boost::multi_index::ordered_unique<
      boost::multi_index::tag<name_tag>, boost::multi_index::member<Foo, std::string, &Foo::name> >
    >
  >
{ };

int main()
{
  ExampleContainer ec1;
  ExampleContainer2 ec2;
}
mindriot
  • 5,413
  • 1
  • 25
  • 34