gpt4 book ai didi

c++ - 使用 GCC 编译 boost::multi_index 和 std::shared_ptr

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:45:07 26 4
gpt4 key购买 nike

此代码使用 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>

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

最佳答案

Looks like a QoI issue with GNU libstdc++. – sehe 1 hour ago

What does that mean? Is there anything I can do about it? – ziv 15 secs ago

它的意思是“实现质量”,这意味着库实现中未指定的细节让生活变得比您想要的更难(可能是由于 namespace 版本控制,但我猜测有点)。

我能做什么?

看起来你只想使用指针身份,这样更容易:

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 编译 boost::multi_index 和 std::shared_ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40282154/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com