Boost logo

Boost Users :

Subject: Re: [Boost-users] Insertion/Use of boost::multi_index
From: Joaquin M López Muñoz (joaquinlopezmunoz_at_[hidden])
Date: 2016-09-19 03:29:43


El 19/09/2016 a las 7:54, Ram escribió:
> Hi All ,
>
> I have created a boost::multi_index container for my use and it
> compiles successfully.
> [...] The container I defined is ,
>
> typedef boost::multi_index_ClassX<
> ClassX*,
> boost::multi_index::indexed_by<
> boost::multi_index::hashed_unique<
> boost::multi_index::const_mem_fun<ParentOfClassX, Int64,
> &ParentOfClassX::getKey1>,
> boost::multi_index::const_mem_fun<ParentOfClassX, Int64,
> &ParentOfClassX::getKey2>,
> >
> >
> > MultiIndexedClassX;
>

I'm afraid this is seriously wrong (leaving aside the translation typos
such as
"boost::multi_index_ClassX" instead of "boost::multi_index_container" etc.)
The structure of a definition of a multi_index_container is

   multi_index_container<
     element_type,
     indexed_by<
       index_specifier1<index1 args>,
       index_specifier2<index2 args>,
       ...
>
>

where each index_specifier is one of "hashed_unique", "hashed_non_unique",
"ordered_unique", etc. I assume your intention is to have *two* hashed
indices
(by key1 and key2, respectively), so rather than

   indexed_by<
     hashed_unique<const_mem_fun1,const_mem_fun2>
>

(which is what you've written), the definition has to follow this structure:

   indexed_by<
     hashed_unique<const_mem_fun1>,
     hashed_unique<const_mem_fun2>
>

See the difference? Your original definition is just plain wrong, and
the fact that it
seems to compile is sheer luck (or lack of it): once you start trying to
use it,
compile-time errors will pop up. (if you're curious, Boost.MultiIndex is
trying to use
your second const_mem_fun as a hash function of the first const_mem_fun,
which,
of course, makes no sense, but this is what you instructed the lib to do).

I've written a small test program reproducing your scenario that you can
play
with at

http://coliru.stacked-crooked.com/a/456f8546e51a29e6

Note that, here, sample.insert(&a) works without problems.

Joaquín M López Muñoz



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