此代码与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>

和c声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也可以编译。

最佳答案


这意味着“实现质量”,这意味着库实现中未指定的细节使您的生活变得比您想要的要难(可能是由于命名空间版本控制,但我猜测是这样)。
我能做什么?
看起来您只想使用指针标识,这更简单:

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";
}

关于c++ - 用GCC编译带有std::shared_ptr的boost::multi_index,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40282154/

10-14 09:19
查看更多