Boost logo

Boost Users :

From: Joaquín Mª López Muñoz (joaquin_at_[hidden])
Date: 2006-12-11 10:13:57


Sunil Chomal ha escrito:

> Dear All, I am a newbie with multi_index container. I need to store
> the following data
> structure in the multi_index_container:

[...]

> struct base
> {
> int base_data;
> };
>
> struct child : public base
> {
> int child_data;
> };
>
> struct base_data{ };
>
> typedef multi_index_container
> <
> child,
> indexed_by<
> ordered_non_unique< tag<base_data>,
> BOOST_MULTI_INDEX_MEMBER(child, int, base_data) >
> >
> > child_set;

[...]

> If i try and compile the above code, I get the following error
> test.cpp:33: error: could not convert template argument
> &base::base_data to int child::*

Hello Sunil,

The problem is that, by a strange rule of C++, &base::base_data (which
is the same as
&child::base_data) is not statically recognized as a pointer to member
of child. The first
time this showed up I posted a request for clarification at
comp.std.c++. with little
success, see http://tinyurl.com/ykuu7w .

You've got two options to solve the problem:

1. Write the key extractor as BOOST_MULTI_INDEX_MEMBER(base, int,
base_data) (or as member<base,int,&base::base_data>). This will only
work in Boost
1.34 (for Boost 1.33.1 and prior you'll get compile errors when trying
to insert
elements into the container), but if you can wait till Bost 1.34 is
released or grab a
CVS snapshot, this is my recommended option.

2. Write a custom key extractor:

struct base_data_extractor
{
  typedef int result_type;

  int operator()(const child &x)const{return x.base_data;}
};

...

typedef multi_index_container
<
  child,
  indexed_by<
    ordered_non_unique<tag<base_data>,base_data_extractor>
>
> child_set;

HTH. Thank you for using Boost.MultiIndex,

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net