编译boost::multi_index与std::shared_ptr与GCC
Compiling boost::multi_index with std::shared_ptr with GCC
此代码使用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";
}
相关文章:
- CLANG 编译器 说:变量"PTR"可能未初始化
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 将 ptr 传递给 ptr 到 A 作为参数传递给 A 的函数是不好的做法吗?
- 为共享 ptr 向量实现复制 c'tor?
- 字符和整数中 **(ptr+1) 的值差异
- C++:在不中断共享的情况下通过引用传递共享 PTR?
- 如何将派生类从基 ptr 分配给 nlohmann::json
- 引用 std::shared:ptr 以避免引用计数
- 为什么我不能在不进行任何转换的情况下将浮点数放入任何类型的 ptr 中?
- 在调用函数时,ptr** 和 ptr*& 之间是否有区别,或者首选C++?
- 另一种类型的智能ptr,比如具有弱refs的unique_ptr
- 尝试打印出 *ptr++ 的值,以了解它是如何工作的
- 如何控制共享 ptr 引用计数?
- dopen():不以 root 身份运行时"failed to map segment from shared object"
- C++中的指针否定 (!ptr == NULL)
- 从const ptr*转换为ptr*时出现问题
- 无法使用 libtool 将 -shared 参数传递给 g++
- boost::shared_ptr和std::shared-ptr的同居
- 我可以用std::shared_ptr而不是boost::shared-ptr构建boost库吗
- shared-ptr-C++shared_ptr与unique_ptr用于资源管理