Boost logo

Boost Users :

Subject: [Boost-users] [multi_index] Introducing C++17 terse key specification syntax
From: Joaquin M López Muñoz (joaquinlopezmunoz_at_[hidden])
Date: 2018-08-22 10:57:29


Following some ideas discussed about a year ago, I've added a new
boost::multi_index::key construct allowing for a very terse
specification of predefined
key extractors in C++17 compliant environments. For instance, given

 Â Â Â  struct phonebook_entry
 Â Â Â  {
 Â Â Â Â Â  std::string family_name;
 Â Â Â Â Â  std::string given_name;
 Â Â Â Â Â  std::string phone_number()const;
 Â Â Â  };

 Â Â Â  int num_calls(const phonebook_entry&);

a classical definition like

 Â Â Â  typedef multi_index_container<
 Â Â Â Â Â  phonebook_entry,
 Â Â Â Â Â  indexed_by<
 Â Â Â Â Â Â Â  ordered_unique<
const_mem_fun<phonebook_entry,std::string,&phonebook_entry::phone_number>
 Â Â Â Â Â Â Â  >,
 Â Â Â Â Â Â Â  ordered_unique<
 Â Â Â Â Â Â Â Â Â  global_fun<const phonebook_entry&,int,num_calls>
 Â Â Â Â Â Â Â  >,
 Â Â Â Â Â Â Â  ordered_non_unique<
 Â Â Â Â Â Â Â Â Â  composite_key<
 Â Â Â Â Â Â Â Â Â Â Â  phonebook_entry,
member<phonebook_entry,std::string,&phonebook_entry::family_name>,
member<phonebook_entry,std::string,&phonebook_entry::given_name>
 Â Â Â Â Â Â Â Â Â  >
 Â Â Â Â Â Â Â  >
 Â Â Â Â Â  >
 Â Â Â  > phonebook;

can now be written as

 Â Â Â  typedef multi_index_container<
 Â Â Â Â Â  phonebook_entry,
 Â Â Â Â Â  indexed_by<
 Â Â Â Â Â Â Â  ordered_unique<
 Â Â Â Â Â Â Â Â Â  key<&phonebook_entry::phone_number>
 Â Â Â Â Â Â Â  >,
 Â Â Â Â Â Â Â  ordered_unique<
 Â Â Â Â Â Â Â Â Â  key<num_calls>
 Â Â Â Â Â Â Â  >,
 Â Â Â Â Â Â Â  ordered_non_unique<
key<&phonebook_entry::family_name,&phonebook_entry::given_name>
 Â Â Â Â Â Â Â  >
 Â Â Â Â Â  >
 Â Â Â  > phonebook;

If you have a C++17 compiler, you can play with this new feature by
simply copying

https://github.com/boostorg/multi_index/blob/develop/include/boost/multi_index/key.hpp

into your local Boost installation. More info at

https://www.boost.org/doc/libs/develop/libs/multi_index/doc/tutorial/key_extraction.html#key
https://www.boost.org/doc/libs/develop/libs/multi_index/doc/reference/key_extraction.html#key_synopsis

Comments and suggestions, specially before Boost 1.69 ships and this becomes
more or less frozen, are most welcome.

Best regards,

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