C++ C++;Boost多指标类型识别
在boost multi-index中,我是否可以通过元编程验证特定索引类型是否有序? 有顺序索引、散列索引、序列索引等。我可以通过元编程找到它们吗 假设有如下索引:C++ C++;Boost多指标类型识别,c++,boost,multi-index,C++,Boost,Multi Index,在boost multi-index中,我是否可以通过元编程验证特定索引类型是否有序? 有顺序索引、散列索引、序列索引等。我可以通过元编程找到它们吗 假设有如下索引: int main() { typedef multi_index_container<double> double_set; return 0; } intmain() { typedef多索引容器双集合; 返回0; } 我想知道double_集索引是有序的、散列的还是排序的。当然,在这种情况
int main()
{
typedef multi_index_container<double> double_set;
return 0;
}
intmain()
{
typedef多索引容器双集合;
返回0;
}
我想知道double_集索引是有序的、散列的还是排序的。当然,在这种情况下,它是订购的。是:
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/mpl/or.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/not.hpp>
#include <boost/mpl/distance.hpp>
#include <boost/mpl/begin.hpp>
namespace mpl = boost::mpl;
namespace mi = boost::multi_index;
//
// checking for ordered_unique:
//
template <typename MI>
struct is_nth_index_ordered_unique_helper : mpl::false_ {};
template <typename KeyFromValue, typename Compare>
struct is_nth_index_ordered_unique_helper<
mi::ordered_unique<KeyFromValue,Compare>
> : mpl::true_ {};
template <typename TagList, typename KeyFromValue, typename Compare>
struct is_nth_index_ordered_unique_helper<
mi::ordered_unique<TagList,KeyFromValue,Compare>
> : mpl::true_ {};
template <typename MI, int N>
struct is_nth_index_ordered_unique
: is_nth_index_ordered_unique_helper<
typename mpl::at_c< typename MI::index_specifier_type_list, N >::type
> {};
//
// checking for ordered_non_unique:
//
template <typename MI>
struct is_nth_index_ordered_non_unique_helper : mpl::false_ {};
template <typename KeyFromValue, typename Compare>
struct is_nth_index_ordered_non_unique_helper<
mi::ordered_non_unique<KeyFromValue,Compare>
> : mpl::true_ {};
template <typename TagList, typename KeyFromValue, typename Compare>
struct is_nth_index_ordered_non_unique_helper<
mi::ordered_non_unique<TagList,KeyFromValue,Compare>
> : mpl::true_ {};
template <typename MI, int N>
struct is_nth_index_ordered_non_unique
: is_nth_index_ordered_non_unique_helper<
typename mpl::at_c< typename MI::index_specifier_type_list, N >::type
> {};
//
// Combined (ordered_{non_,}unique):
//
template <typename MI, int N>
struct is_nth_index_ordered
: mpl::or_<
is_nth_index_ordered_unique<MI,N>,
is_nth_index_ordered_non_unique<MI,N>
> {};
//
// checking for sequenced:
//
template <typename MI>
struct is_nth_index_sequenced_helper : mpl::false_ {};
template <typename TagList>
struct is_nth_index_sequenced_helper<
mi::sequenced<TagList>
> : mpl::true_ {};
template <typename MI, int N>
struct is_nth_index_sequenced
: is_nth_index_sequenced_helper<
typename mpl::at_c< typename MI::index_specifier_type_list, N >::type
> {};
//
// test with example container:
//
typedef mi::multi_index_container<double> double_set_1;
BOOST_MPL_ASSERT(( is_nth_index_ordered<double_set_1,0> ));
BOOST_MPL_ASSERT(( mpl::not_< is_nth_index_sequenced<double_set_1,0> > ));
// or
BOOST_STATIC_ASSERT(( is_nth_index_ordered<double_set_1,0>::value ));
BOOST_STATIC_ASSERT(( mpl::not_< is_nth_index_sequenced<double_set_1,0> >::value ));
//
// And now with tag dispatch:
//
template <typename MI, typename Tag>
struct tag_to_n
: mpl::distance<
typename mpl::begin<typename MI::index_type_list>::type,
typename MI::template index<Tag>::iter
> {};
template <typename MI, typename Tag>
struct is_tagged_index_ordered_unique
: is_nth_index_ordered_unique<MI,tag_to_n<MI,Tag>::value> {};
template <typename MI, typename Tag>
struct is_tagged_index_ordered_non_unique
: is_nth_index_ordered_non_unique<MI,tag_to_n<MI,Tag>::value> {};
template <typename MI, typename Tag>
struct is_tagged_index_ordered
: is_nth_index_ordered<MI,tag_to_n<MI,Tag>::value> {};
template <typename MI, typename Tag>
struct is_tagged_index_sequenced
: is_nth_index_sequenced<MI,tag_to_n<MI,Tag>::value> {};
//
// test with another example container:
//
struct as_set {};
struct as_list {};
typedef mi::multi_index_container<
double,
mi::indexed_by<
mi::sequenced< mi::tag<as_list> >,
mi::ordered_non_unique< mi::tag<as_set>, mi::identity<double> >
>
> double_set_2;
void fun() {
double_set_2 ds2;
}
BOOST_MPL_ASSERT(( is_nth_index_sequenced<double_set_2,0> ));
BOOST_MPL_ASSERT(( is_nth_index_ordered<double_set_2,1> ));
BOOST_MPL_ASSERT(( mpl::not_< is_nth_index_ordered<double_set_2,0> > ));
BOOST_MPL_ASSERT(( mpl::not_< is_nth_index_sequenced<double_set_2,1> > ));
BOOST_MPL_ASSERT(( is_tagged_index_sequenced<double_set_2,as_list> ));
BOOST_MPL_ASSERT(( is_tagged_index_ordered<double_set_2,as_set> ));
BOOST_MPL_ASSERT(( mpl::not_< is_tagged_index_ordered<double_set_2,as_list> > ));
BOOST_MPL_ASSERT(( mpl::not_< is_tagged_index_sequenced<double_set_2,as_set> > ));
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
名称空间mpl=boost::mpl;
名称空间mi=boost::multi_索引;
//
//正在检查订单的唯一性:
//
模板
结构是第n个索引、有序、唯一的辅助对象:mpl::false{};
模板
结构是第n个索引、有序、唯一的辅助对象<
mi::有序_唯一
>:mpl::true{};
模板
结构是第n个索引、有序、唯一的辅助对象<
mi::有序_唯一
>:mpl::true{};
模板
结构是第n个索引有序唯一的
:第n个索引是否有序唯一<
typename mpl::at_c::type
> {};
//
//检查有序的\u非\u唯一:
//
模板
结构是第n个索引有序的非唯一的辅助对象:mpl::false{};
模板
结构是第n个索引有序非唯一辅助对象<
mi::有序_非唯一
>:mpl::true{};
模板
结构是第n个索引有序非唯一辅助对象<
mi::有序_非唯一
>:mpl::true{};
模板
结构是第n个索引有序的非唯一的
:第n个索引是有序的吗?非唯一的吗<
typename mpl::at_c::type
> {};
//
//组合(有序{非}唯一):
//
模板
结构是第n个索引
:mpl::或_<
第n个索引是唯一的吗,
第n个索引是有序的还是非唯一的
> {};
//
//检查序列:
//
模板
struct是第n个索引排序的助手:mpl::false{};
模板
结构是第n个索引,按顺序排列<
mi::测序
>:mpl::true{};
模板
结构是第n个索引
:第n个索引是否已排序<
typename mpl::at_c::type
> {};
//
//使用示例容器进行测试:
//
类型定义mi::多索引容器双集1;
BOOST_MPL_ASSERT((是第n个索引排序的));
BOOST\u MPL\u ASSERT((MPL::not);
//或
BOOST_STATIC_ASSERT((是第n个索引的顺序::值));
BOOST_STATIC_ASSERT((mpl::not_::value));
//
//现在是标签调度:
//
模板
结构标记到
:mpl::距离<
typename mpl::begin::type,
typename MI::模板索引::iter
> {};
模板
结构是\u标记的\u索引\u排序的\u唯一的
:是第n个索引有序唯一{};
模板
结构是\u标记的\u索引\u有序的\u非唯一的
:是第n个索引有序的非唯一的{};
模板
结构已标记\u索引\u排序
:是第n个索引{};
模板
结构已\u标记\u索引\u排序
:是第n个索引的序列{};
//
//使用另一个示例容器进行测试:
//
结构为_集{};
结构为_列表{};
类型定义mi::多索引容器<
双重的
mi::索引为<
mi::sequenced,
mi::有序的\非\唯一的
>
>双_集_2;
虚无乐趣(){
双_集_2 ds2;
}
BOOST_MPL_ASSERT((第n个索引是否已排序));
BOOST_MPL_ASSERT((是第n个索引排序的));
BOOST\u MPL\u ASSERT((MPL::not<是第n个索引>);
BOOST\u MPL\u ASSERT((MPL::not);
BOOST_MPL_ASSERT((是否已标记索引顺序));
BOOST_MPL_ASSERT((标记索引顺序));
BOOST_MPL_ASSERT((MPL::not_u);
BOOST\u MPL\u ASSERT((MPL::not);
您能展示一下您想如何使用它吗?