编译boost::multi_index与std::shared_ptr与GCC

Compiling boost::multi_index with std::shared_ptr with GCC

本文关键字:shared ptr GCC std index boost multi 编译      更新时间:2023-10-16

此代码使用visual studio 2015进行编译。

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/key_extractors.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <memory>
namespace bmi = boost::multi_index;
class Link {};
typedef std::shared_ptr<Link> Link_Ptr;

struct ByRnd {};
struct ByPtr {};
typedef boost::multi_index_container<Link_Ptr,
    bmi::indexed_by<
        bmi::random_access<
            bmi::tag<ByRnd>
        >,
        bmi::hashed_unique<
            bmi::tag<ByPtr>,
            bmi::const_mem_fun<Link_Ptr, Link*, &Link_Ptr::get>
        >
    >
> Store;
Store store;
int main() {}

然而,在Ubuntu上我使用GCC 6.2和boost 1.62。我得到以下错误:

error:   
could not convert template argument ‘&std::__shared_ptr<Link, (__gnu_cxx::_Lock_policy)2u>::get’ to ‘Link* (std::shared_ptr<Link>::*)() const’
    bmi::const_mem_fun<Link_Ptr, Link*, &Link_Ptr::get>

and clang 3.8:

error:  
non-type template argument of type 'Link *(std::__shared_ptr<Link, __gnu_cxx::_Lock_policy::_S_atomic>::*)() const noexcept' cannot be converted to a value of type
      'Link *(std::shared_ptr<Link>::*)() const'
                        bmi::const_mem_fun<Link_Ptr, Link*, &Link_Ptr::get>
如果我使用boost::shared_ptr, GCC也可以很好地编译。

看起来像是GNU libstdc++的qi问题。- sehe 1 hour ago

那是什么意思?我能做点什么吗?- ziv 15 secs ago

它的意思是"实现质量",意思是库实现中一个未指定的细节使生活比你想要的更困难(可能是由于名称空间版本控制,但我猜有一点)。

我能做什么?

看起来您只是想使用指针标识,这更容易:

bmi::hashed_unique<bmi::tag<struct ByPtr>, bmi::identity<Link_Ptr> >

Live On Coliru

#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/key_extractors.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index_container.hpp>
#include <memory>
#include <iostream>
#include <cassert>
namespace bmi = boost::multi_index;
class Link {};
typedef std::shared_ptr<Link> Link_Ptr;
typedef boost::multi_index_container<
    Link_Ptr,
    bmi::indexed_by<bmi::random_access<bmi::tag<struct ByRnd> >,
                    bmi::hashed_unique<bmi::tag<struct ByPtr>, bmi::identity<Link_Ptr> > > >
    Store;
Store store;
int main() {
    auto a = std::make_shared<Link>();
    auto b = a; // same
    auto& idx = store.get<ByPtr>();
    idx.insert(a);
    auto res = idx.insert(b);
    assert(!res.second);
    std::cout << "effective number of links: " << store.size() << "n";
}

或者

你可以使用bmi::global_fun:

template <typename T> static T *get_ptr(std::shared_ptr<T> const &p) 
{ return p.get(); }

Live On Coliru

#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/key_extractors.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index_container.hpp>
#include <cassert>
#include <iostream>
#include <memory>
namespace bmi = boost::multi_index;
class Link {};
typedef std::shared_ptr<Link> Link_Ptr;
template <typename T> static T *get_ptr(std::shared_ptr<T> const &p) { return p.get(); }
typedef boost::multi_index_container<
    Link_Ptr, bmi::indexed_by<bmi::random_access<bmi::tag<struct ByRnd> >,
                              bmi::hashed_unique<bmi::tag<struct ByPtr>,
                                                 bmi::global_fun<Link_Ptr const &, Link *, &get_ptr<Link> > > > >
    Store;
Store store;
int main() {
    auto a = std::make_shared<Link>();
    auto b = a; // same
    auto &idx = store.get<ByPtr>();
    idx.insert(a);
    auto res = idx.insert(b);
    assert(!res.second);
    std::cout << "effective number of links: " << store.size() << "n";
}