Search code examples
stlcontainersc++17visual-studio-2019boost-multi-index

How to improve readability of multi_index_container for programmers not used to it?


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?


Solution

  • 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;
    };