1

I am using boost::multi-index container for a collection of tuples, and I would like to create an index on the first element of the tuple. Here is my solution by creating an wrapper function getFirst and pass it to the template parameter of the multi_index_container as the global_fun

This solution works, but I am wondering if it is possible to use std::get directly, without defining another wrapper function.

namespace {
    using my_tuple_t  = std::tuple<int, double>;

    int getFirst(my_tuple_t x) {
        return std::get<0>(x);
    }

    struct first {
    };

    using my_container = bmi::multi_index_container<
            my_tuple_t,
            bmi::indexed_by<
                    bmi::ordered_unique<
                            bmi::tag<struct first>,
                            bmi::global_fun<my_tuple_t, int, &getFirst>
                    >
            >
    >;
}
motam79
  • 3,542
  • 5
  • 34
  • 60

1 Answers1

1

You can use std::get directly, but what you get is more verbose than simply wrapping the thing as you've done:

using my_container = bmi::multi_index_container<
  my_tuple_t,
  bmi::indexed_by<
    bmi::ordered_unique<
      bmi::tag<struct first>,
      bmi::global_fun<const my_tuple_t&, const int&, &std::get<0, int, double>>
    >
  >
>;

The problem is that std::get is not a function but an overload set of function templates, and global_fun expects a concrete function pointer, so to get that you need to instantiate std::get with all its template arguments explicitly written down. Check for instance cppreference.com for an explanation of the template parameters involved.

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