0

I am trying to solve an issue using Boost multi_index. If I have 2 structures as follows:

struct MyStruct1
{
    int x;
    int y;
};

struct MyStruct2
{
   int a;
   MyStruct1 b;
};

How would I define an index using MyStruct2::b.x? Is this possible?

was trying something like:

struct xIndex{};

typedef multi_index_container<
    MyStruct2,
    indexed_by<
        ordered_unique<
            tag<xIndex>,
            member<MyStruct2, int, &MyStruct2::a::x>
        >
    >
> MyContainer;

But that doesn't work.

Thanks for any info/advice.

1 Answers1

1

There are several ways to accomplish this but all of them require that you write some boilerplate code. The easiest one is providing a user-defined key extractor:

struct MyStruct2XExtractor
{
  typedef int result_type;

  int operator()(const MyStruct2& m)const
  {
    return m.b.x;
  }
};

...

typedef multi_index_container<
    MyStruct2,
    indexed_by<
        ordered_unique<
            tag<xIndex>,
            MyStruct2XExtractor
        >
    >
> MyContainer;
Joaquín M López Muñoz
  • 5,243
  • 1
  • 15
  • 20