[multi-index] multi-index type trait

Hi how to test if the first index of boost::multi-index is key based or sequence based at compile time. I need to write something like this : template<class T,class M> void f(T& object,typename boost::enable_if<first_index_is_sequence<T>>::type* dummy = 0) { M element; object.push_back(element); } template<class T,class M> void f(T& object,typename boost::enable_if<first_index_is_key<T>>::type* dummy = 0) { M element; object.insert(element); } -- View this message in context: http://boost.2283326.n4.nabble.com/multi-index-multi-index-type-trait-tp4678... Sent from the Boost - Users mailing list archive at Nabble.com.

Elizabeta <elizabeta.petreska <at> gmail.com> writes:
Hi how to test if the first index of boost::multi-index is key based or sequence based at compile time[...]
This can do: #include <boost/multi_index/ordered_index_fwd.hpp> #include <boost/multi_index/hashed_index_fwd.hpp> #include <boost/mpl/bool.hpp> #include <boost/mpl/front.hpp> template<typename Index> struct is_key_based_index:boost::mpl::false_{}; template<typename... Args> struct is_key_based_index< boost::multi_index::ordered_unique<Args...> >:boost::mpl::true_{}; template<typename... Args> struct is_key_based_index< boost::multi_index::ordered_non_unique<Args...> >:boost::mpl::true_{}; template<typename... Args> struct is_key_based_index< boost::multi_index::hashed_unique<Args...> >:boost::mpl::true_{}; template<typename... Args> struct is_key_based_index< boost::multi_index::hashed_non_unique<Args...> >:boost::mpl::true_{}; template<typename MultiIndexContainer> struct is_first_index_key_based: is_key_based_index< typename boost::mpl::front< typename MultiIndexContainer::index_specifier_type_list >::type > {}; Full example at http://coliru.stacked-crooked.com/a/936add104162025d Joaquín M López Muñoz Telefónica

Joaquin M Lopez Munoz <joaquin <at> tid.es> writes:
Elizabeta <elizabeta.petreska <at> gmail.com> writes:
Hi how to test if the first index of boost::multi-index is key based or sequence based at compile time[...]
This can do:
Correction: this is terser and is prepared for Boost 1.59 (which includes a new type of key-based indices, namely ranked indices): #include <boost/multi_index/sequenced_index_fwd.hpp> #include <boost/multi_index/random_access_index_fwd.hpp> #include <boost/mpl/bool.hpp> #include <boost/mpl/front.hpp> template<typename Index> struct is_key_based_index:boost::mpl::true_{}; template<typename... Args> struct is_key_based_index< boost::multi_index::sequenced<Args...> >:boost::mpl::false_{}; template<typename... Args> struct is_key_based_index< boost::multi_index::random_access<Args...> >:boost::mpl::false_{}; template<typename MultiIndexContainer> struct is_first_index_key_based: is_key_based_index< typename boost::mpl::front< typename MultiIndexContainer::index_specifier_type_list >::type > {}; Full example at http://coliru.stacked-crooked.com/a/5a01356f0908fd9a Joaquín M López Muñoz Telefónica

Joaquin M Lopez Munoz <joaquin <at> tid.es> writes:
Joaquin M Lopez Munoz <joaquin <at> tid.es> writes:
Elizabeta <elizabeta.petreska <at> gmail.com> writes:
Hi how to test if the first index of boost::multi-index is key based or sequence based at compile time[...]
This can do:
Correction: this is terser and is prepared for Boost 1.59 (which includes a new type of key-based indices, namely ranked indices):
Just for the fun of it, the following variation is more future-proof in the sense that it does not depend on the particular catalog of indices that Boost.MultiIndex is providing for a given Boost version: #include <boost/multi_index_container_fwd.hpp> #include <boost/mpl/bool.hpp> #include <boost/mpl/front.hpp> template<typename Value,typename Index> using mono_index_container= boost::multi_index_container< Value, boost::multi_index::indexed_by<Index> >; template<typename T> struct void_t { typedef void type; }; template<typename MultiIndexContainer,typename=void> struct is_first_index_key_based:boost::mpl::false_{}; template<typename MultiIndexContainer> struct is_first_index_key_based< MultiIndexContainer, typename void_t< typename mono_index_container< typename MultiIndexContainer::value_type, typename boost::mpl::front< typename MultiIndexContainer::index_specifier_type_list >::type >::key_type >::type >:boost::mpl::true_{}; This uses Walter Brown's void_t technique to determine whether a multi_index_container, constructed out of the first index of a different, user-provided multi_index_container, has an embedded key_type. The mono_index_container intermediate step is needed because otherwise key_type might exist but be inaccesible (for instance if the first index is sequenced and the second ordered). All in all, you're better off with the previous code, as this is likely to take longer to compile. Full example at http://coliru.stacked-crooked.com/a/42912fc5803143e2 Joaquín M López Muñoz Telefónica

Hi thank you for you reply. This is straightforward solution, but how to the same without using variadic templates. I am using vs2010 and it doesn't support them -- View this message in context: http://boost.2283326.n4.nabble.com/multi-index-multi-index-type-trait-tp4678... Sent from the Boost - Users mailing list archive at Nabble.com.

Elizabeta <elizabeta.petreska <at> gmail.com> writes:
Hi thank you for you reply. This is straightforward solution , but how to the same without using variadic templates. I am using vs2010 and it doesn't support them
Please keep some of the post you're answering to in yours, so as to provide context. I can deduce you refer to one of my first two solutions (the third does not use variadic args), but which in particular is lost in the process. Check http://www.boost.org/community/policy.html#quoting for useful info on Boost posting guidelines. As for not using variadic args, simply replace typename... Args with as many template args the index specifier takes (3 for ordered and ranked indices, 4 for hashed indices, 1 for sequenced and random access indices). Joaquín M López Muñoz Telefónica
participants (2)
-
Elizabeta
-
Joaquin M Lopez Munoz