0

I created a multi-index container with 3 non-unique, non-ordered keys looking like following:

namespace bmi = boost::multi_index;
class SurveyRepository {
// some other code
    using SurveyCodeContainer = boost::multi_index_container<
        SurveyCode,
        bmi::indexed_by<
            bmi::hashed_non_unique<bmi::tag<Survey>, bmi::member<SurveyCode, unsigned, &SurveyCode::survey_id>>,
            bmi::hashed_non_unique<bmi::tag<Table>, bmi::member<SurveyCode, unsigned, &SurveyCode::table_id>>,
            bmi::hashed_non_unique<bmi::tag<Check>, bmi::member<SurveyCode, unsigned, &SurveyCode::check_id>>
        >
    >;

    SurveyCodeContainer m_SurveyCodeContainer;
};

The point was to be able to search the SurveyCode objects using any of those keys and I thought it's totally readable and quite a neat solution.

But there was a code review and although the multi_index_container has been already used in our code base in the past, some people have been confused with comments like:

Isnt there a less ugly container?

So is there a way how to make it less ugly/more readable? We are using Visual Studio 2019 and I would prefer some solution from stl instead of boost, but I guess there isn't any, right?

Joaquín M López Muñoz
  • 5,243
  • 1
  • 15
  • 20
Lukas Salich
  • 959
  • 2
  • 12
  • 30
  • 1
    I've never heard of boost::multi_index_container and find that typedef quite understandable after looking at it for like 10 seconds. Just my two cents :). – AVH Feb 27 '20 at 23:54

1 Answers1

1

If you're using C++17, there's a slightly more convenient syntax available:

namespace bmi = boost::multi_index;
class SurveyRepository {
// some other code
   using SurveyCodeContainer = boost::multi_index_container<
        SurveyCode,
        bmi::indexed_by<
            bmi::hashed_non_unique<bmi::tag<Survey>, bmi::key<&SurveyCode::survey_id>>,
            bmi::hashed_non_unique<bmi::tag<Table>, bmi::key<&SurveyCode::table_id>>,
            bmi::hashed_non_unique<bmi::tag<Check>, bmi::key<&SurveyCode::check_id>>
        >
    >;

    SurveyCodeContainer m_SurveyCodeContainer;
};
Joaquín M López Muñoz
  • 5,243
  • 1
  • 15
  • 20