使用带有GCC的std :: shared_ptr编译boost :: multi_index

时间:2016-10-27 10:23:20

标签: c++ c++11 gcc boost

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

和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也会编译好。

1 个答案:

答案 0 :(得分:0)

  

看起来像GNU libstdc ++的QoI问题。 - sehe 1小时前

     

这是什么意思?我能做些什么吗? - ziv 15秒前

这意味着&#34;实施质量&#34;,意味着库实现中未指明的细节使得生活比你想要的更难(可能是由于名称空间版本控制,但我猜这一点)。

我该怎么办?

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

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