[multi_index] Introducing C++17 terse key specification syntax

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[multi_index] Introducing C++17 terse key specification syntax

Boost - Users mailing list
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 mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users