【问题标题】:boost multi index convert index to tag and loop on indexes提升多索引将索引转换为标签并在索引上循环
【发布时间】:2018-08-21 22:53:04
【问题描述】:

我有一个模板类(CrMultiIndex),它接收一个模板参数,一个 boost 多索引(GlobalHash)的定义。我使用 c++14

我需要一种将索引转换为标签(n_to_tag)的方法吗?并循环 CrMultiIndex ctor 或 Init 函数中的索引? 我最初的目的是循环索引并在初始化时使用 typeid(T).name() 生成标签名称字符串。所以我可以根据标签名称显示统计信息

我有模板类

template <typename KeysType, typename MultiIndexType>
class CrMultiIndex
{

    std::vector<SrStatisticsByIndex> m_StatsByIndex;

public:
    MultiIndexType *m_pMultiIndex=NULL; 

    CrMultiIndex()
    {
        m_pMultiIndex = new MultiIndexType(typename 
        MultiIndexType::ctor_args_list());
    }

这里是boost多索引容器的定义:

typedef boost::multi_index::multi_index_container<
  CrUsersKeys,
  UsersKey_hash_indices/*,
  bip::allocator<CrUsersKeys,bip::managed_shared_memory::segment_manager>*/
> GlobalHash;

代码在http://coliru.stacked-crooked.com/a/d97195a6e4bb7ad4

我在Find boost multi index Tag to index and number of indices问了一个类似的问题

【问题讨论】:

    标签: c++ boost-mpl boost-multi-index


    【解决方案1】:

    你可以这样使用:

    Live On Coliru

    template<typename MultiIndexContainer,std::size_t N>
    std::string static_tag_name()
    {
     const std::type_info& i=typeid(
        typename boost::mpl::front<
          typename boost::multi_index::nth_index<MultiIndexContainer,N>::
            type::tag_list
        >::type
      );
      return i.name();
    }
    
    template<typename MultiIndexContainer,std::size_t... I>
    std::string tag_name(std::size_t n,std::index_sequence<I...>)
    {
      static std::array<std::string(*)(),sizeof...(I)> a=
        {{static_tag_name<MultiIndexContainer,I>...}};
      return a[n]();
    }
    
    template<typename MultiIndexContainer>
    std::string tag_name(std::size_t n)
    {
      return tag_name<MultiIndexContainer>(
        n,std::make_index_sequence<
          boost::mpl::size<
            typename MultiIndexContainer::index_type_list
          >::value
        >{}
      );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-04
      • 1970-01-01
      • 2018-10-12
      • 2021-08-28
      • 2011-01-09
      • 2021-12-17
      • 1970-01-01
      • 2019-03-03
      相关资源
      最近更新 更多