提升 multi_index_container 未编译

boost multi_index_container not compiling

尝试按照 boost 文档了解此用法,但 运行 陷入困境。

基于 CentOS 7、g++ 4.8.5-28 构建。语言标准:C++03

工作样本:https://godbolt.org/z/KPvjS_

我的代码:

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/composite_key.hpp>

using namespace ::boost;
using namespace ::boost::multi_index;

typedef unsigned char UInt8;
typedef unsigned int UInt32;
typedef UInt8 ACCESSID[16];

class AccessID
{
public:
    AccessID();
    AccessID(const AccessID & id);
    const std::size_t GetHash() const { return hashId;}

private:
    ACCESSID id;
    std::size_t hashId;
};

struct map_entry
{
    UInt32          subject_hash;
    UInt32          container;
    AccessID        clientAccessId;
    AccessID        serverAccessId;

    map_entry( UInt32   parmHash
             , UInt32   parmContainer
             , AccessID &parmClientAccessId
             , AccessID &parmServerAccessId )
        : subject_hash( parmHash )
        , container( parmContainer )
        , clientAccessId( parmClientAccessId )
        , serverAccessId( parmServerAccessId )
    {
    }
};

struct clientAccessId_extractor
{
    typedef std::size_t result_type;
    result_type operator ()( const map_entry &e ) const
    {
        // Hash is a boost::hash value.
        return e.clientAccessId.GetHash();
    }
};

struct serverAccessId_extractor
{
    typedef std::size_t result_type;
    result_type operator ()( const map_entry &e ) const
    {
        return e.serverAccessId.GetHash();
    }
};

// Define the mapping relationships; like a "database" of sorts.
typedef multi_index_container<
    map_entry,
    indexed_by<
        // Former "global" view.
        ordered_unique<
            composite_key<
                map_entry,
                member< map_entry, UInt32, &map_entry::container >,
                serverAccessId_extractor
            >
        >,

        // Regular lookup.
        ordered_unique<
            composite_key<
                map_entry,
                member< map_entry, UInt32, &map_entry::subject_hash >,
                member< map_entry, UInt32, &map_entry::container >,
                clientAccessId_extractor
            >
        >
    >
> mapDb_t;

// Typedef the index types for easier reading.
typedef mapDb_t::nth_index< 0 >::type mapDb_by_serverAid;
typedef mapDb_t::nth_index< 1 >::type mapDb_by_clientAid;

class MappingClass
{
private:

    // Index views.
    mapDb_by_serverAid serverView;
    mapDb_by_clientAid clientView;

    // Index iterators.
    mapDb_by_clientAid::iterator clientIter;
    mapDb_by_serverAid::iterator serverIter;

    // Define an object of the mapping db type.
    mapDb_t mapDb;

public:
    MappingClass() { };
    ~MappingClass() { };
};

以及尝试编译的错误输出:

$ g++ -fPIC -x c++ -c -Wno-unused-local-typedefs -Wall -Werror -O2 -m64 -D__EXTENSIONS__ -U_RWSTD_MULTI_THREAD -U_REENTRANT -DUSE_PTHREADS -DNDEBUG -DGCCBOOL -U_THREAD_SAFE -I/home/jearle/git//Open-Source/boost/1.51.0/common  test_mi2.cpp -o test_mi2.o
test_mi2.cpp: In constructor ‘MappingClass::MappingClass()’:
test_mi2.cpp:110:20: error: no matching function for call to ‘boost::multi_index::detail::ordered_index<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, serverAccessId_extractor>, std::less<boost::multi_index::composite_key_result<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, serverAccessId_extractor> > >, boost::multi_index::detail::nth_layer<1, map_entry, boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, serverAccessId_extractor> >, boost::multi_index::ordered_unique<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor> > >, std::allocator<map_entry> >, boost::mpl::vector0<mpl_::na>, boost::multi_index::detail::ordered_unique_tag>::ordered_index( ’
     MappingClass() { };
                    ^
test_mi2.cpp:110:20: note: candidates are:
In file included from test_mi2.cpp:3:0:
/home/jearle/git//Open-Source/boost/1.51.0/common/boost/multi_index/ordered_index.hpp:542:3: note: boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta, TagList, Category>::ordered_index(const boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta, TagList, Category>&) [with KeyFromValue = boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, serverAccessId_extractor>; Compare = std::less<boost::multi_index::composite_key_result<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, serverAccessId_extractor> > >; SuperMeta = boost::multi_index::detail::nth_layer<1, map_entry, boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, serverAccessId_extractor> >, boost::multi_index::ordered_unique<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor> > >, std::allocator<map_entry> >; TagList = boost::mpl::vector0<mpl_::na>; Category = boost::multi_index::detail::ordered_unique_tag]
   ordered_index(
   ^
/home/jearle/git//Open-Source/boost/1.51.0/common/boost/multi_index/ordered_index.hpp:542:3: note:   candidate expects 1 argument, 0 provided
/home/jearle/git//Open-Source/boost/1.51.0/common/boost/multi_index/ordered_index.hpp:534:3: note: boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta, TagList, Category>::ordered_index(const ctor_args_list&, const allocator_type&) [with KeyFromValue = boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, serverAccessId_extractor>; Compare = std::less<boost::multi_index::composite_key_result<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, serverAccessId_extractor> > >; SuperMeta = boost::multi_index::detail::nth_layer<1, map_entry, boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, serverAccessId_extractor> >, boost::multi_index::ordered_unique<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor> > >, std::allocator<map_entry> >; TagList = boost::mpl::vector0<mpl_::na>; Category = boost::multi_index::detail::ordered_unique_tag; boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta, TagList, Category>::ctor_args_list = boost::tuples::cons<boost::tuples::tuple<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, serverAccessId_extractor>, std::less<boost::multi_index::composite_key_result<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, serverAccessId_extractor> > >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>, boost::tuples::cons<boost::tuples::tuple<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor>, std::less<boost::multi_index::composite_key_result<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor> > >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>, boost::tuples::null_type> >; typename SuperMeta::type::ctor_args_list = boost::tuples::cons<boost::tuples::tuple<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor>, std::less<boost::multi_index::composite_key_result<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor> > >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>, boost::tuples::null_type>; boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta, TagList, Category>::allocator_type = std::allocator<map_entry>]
   ordered_index(const ctor_args_list& args_list,const allocator_type& al):
   ^
/home/jearle/git//Open-Source/boost/1.51.0/common/boost/multi_index/ordered_index.hpp:534:3: note:   candidate expects 2 arguments, 0 provided
/home/jearle/git//Open-Source/boost/1.51.0/common/boost/multi_index/ordered_index.hpp:558:3: error: ‘boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta, TagList, Category>::~ordered_index() [with KeyFromValue = boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, serverAccessId_extractor>; Compare = std::less<boost::multi_index::composite_key_result<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, serverAccessId_extractor> > >; SuperMeta = boost::multi_index::detail::nth_layer<1, map_entry, boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, serverAccessId_extractor> >, boost::multi_index::ordered_unique<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor> > >, std::allocator<map_entry> >; TagList = boost::mpl::vector0<mpl_::na>; Category = boost::multi_index::detail::ordered_unique_tag]’ is protected
   ~ordered_index()
   ^
test_mi2.cpp:110:20: error: within this context
     MappingClass() { };
                    ^
test_mi2.cpp:110:20: error: no matching function for call to ‘boost::multi_index::detail::ordered_index<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor>, std::less<boost::multi_index::composite_key_result<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor> > >, boost::multi_index::detail::nth_layer<2, map_entry, boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, serverAccessId_extractor> >, boost::multi_index::ordered_unique<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor> > >, std::allocator<map_entry> >, boost::mpl::vector0<mpl_::na>, boost::multi_index::detail::ordered_unique_tag>::ordered_index()’
test_mi2.cpp:110:20: note: candidates are:
In file included from test_mi2.cpp:3:0:
/home/jearle/git//Open-Source/boost/1.51.0/common/boost/multi_index/ordered_index.hpp:542:3: note: boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta, TagList, Category>::ordered_index(const boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta, TagList, Category>&) [with KeyFromValue = boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor>; Compare = std::less<boost::multi_index::composite_key_result<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor> > >; SuperMeta = boost::multi_index::detail::nth_layer<2, map_entry, boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, serverAccessId_extractor> >, boost::multi_index::ordered_unique<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor> > >, std::allocator<map_entry> >; TagList = boost::mpl::vector0<mpl_::na>; Category = boost::multi_index::detail::ordered_unique_tag]
   ordered_index(
   ^
/home/jearle/git//Open-Source/boost/1.51.0/common/boost/multi_index/ordered_index.hpp:542:3: note:   candidate expects 1 argument, 0 provided
/home/jearle/git//Open-Source/boost/1.51.0/common/boost/multi_index/ordered_index.hpp:534:3: note: boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta, TagList, Category>::ordered_index(const ctor_args_list&, const allocator_type&) [with KeyFromValue = boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor>; Compare = std::less<boost::multi_index::composite_key_result<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor> > >; SuperMeta = boost::multi_index::detail::nth_layer<2, map_entry, boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, serverAccessId_extractor> >, boost::multi_index::ordered_unique<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor> > >, std::allocator<map_entry> >; TagList = boost::mpl::vector0<mpl_::na>; Category = boost::multi_index::detail::ordered_unique_tag; boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta, TagList, Category>::ctor_args_list = boost::tuples::cons<boost::tuples::tuple<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor>, std::less<boost::multi_index::composite_key_result<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor> > >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>, boost::tuples::null_type>; typename SuperMeta::type::ctor_args_list = boost::tuples::null_type; boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta, TagList, Category>::allocator_type = std::allocator<map_entry>]
   ordered_index(const ctor_args_list& args_list,const allocator_type& al):
   ^
/home/jearle/git//Open-Source/boost/1.51.0/common/boost/multi_index/ordered_index.hpp:534:3: note:   candidate expects 2 arguments, 0 provided
/home/jearle/git//Open-Source/boost/1.51.0/common/boost/multi_index/ordered_index.hpp:558:3: error: ‘boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta, TagList, Category>::~ordered_index() [with KeyFromValue = boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor>; Compare = std::less<boost::multi_index::composite_key_result<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor> > >; SuperMeta = boost::multi_index::detail::nth_layer<2, map_entry, boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, serverAccessId_extractor> >, boost::multi_index::ordered_unique<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor> > >, std::allocator<map_entry> >; TagList = boost::mpl::vector0<mpl_::na>; Category = boost::multi_index::detail::ordered_unique_tag]’ is protected
   ~ordered_index()
   ^
test_mi2.cpp:110:20: error: within this context
     MappingClass() { };
                    ^
In file included from test_mi2.cpp:3:0:
/home/jearle/git//Open-Source/boost/1.51.0/common/boost/multi_index/ordered_index.hpp: In destructor ‘MappingClass::~MappingClass()’:
/home/jearle/git//Open-Source/boost/1.51.0/common/boost/multi_index/ordered_index.hpp:558:3: error: ‘boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta, TagList, Category>::~ordered_index() [with KeyFromValue = boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, serverAccessId_extractor>; Compare = std::less<boost::multi_index::composite_key_result<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, serverAccessId_extractor> > >; SuperMeta = boost::multi_index::detail::nth_layer<1, map_entry, boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, serverAccessId_extractor> >, boost::multi_index::ordered_unique<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor> > >, std::allocator<map_entry> >; TagList = boost::mpl::vector0<mpl_::na>; Category = boost::multi_index::detail::ordered_unique_tag]’ is protected
   ~ordered_index()
   ^
test_mi2.cpp:111:21: error: within this context
     ~MappingClass() { };
                     ^
In file included from test_mi2.cpp:3:0:
/home/jearle/git//Open-Source/boost/1.51.0/common/boost/multi_index/ordered_index.hpp:558:3: error: ‘boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta, TagList, Category>::~ordered_index() [with KeyFromValue = boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor>; Compare = std::less<boost::multi_index::composite_key_result<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor> > >; SuperMeta = boost::multi_index::detail::nth_layer<2, map_entry, boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, serverAccessId_extractor> >, boost::multi_index::ordered_unique<boost::multi_index::composite_key<map_entry, boost::multi_index::member<map_entry, unsigned int, &map_entry::subject_hash>, boost::multi_index::member<map_entry, unsigned int, &map_entry::container>, clientAccessId_extractor> > >, std::allocator<map_entry> >; TagList = boost::mpl::vector0<mpl_::na>; Category = boost::multi_index::detail::ordered_unique_tag]’ is protected
   ~ordered_index()
   ^
test_mi2.cpp:111:21: error: within this context
     ~MappingClass() { };
                     ^

我真的不明白这里的提升错误是什么。我错过了什么?

地图索引类型不是默认可构造的,您需要将它们更改为引用并在构造函数中初始化它们:

class MappingClass
{
private:
    // Define an object of the mapping db type.
    mapDb_t mapDb;

    // Index iterators.
    mapDb_by_clientAid::iterator clientIter;
    mapDb_by_serverAid::iterator serverIter;

    // Index views.
    mapDb_by_serverAid& serverView;
    mapDb_by_clientAid& clientView;

public:
    MappingClass()
    :serverView( mapDb.get< 0 >() ),
     clientView( mapDb.get< 1 >() ) { };
    ~MappingClass() { };
};

请注意,您还需要将 mapDb 移动到视图之前,以便首先对其进行初始化。