boost::make_shared<T>(...) 不编译, shared_ptr<T>(new T(...)) 编译

标签 boost c++11 shared-ptr

我在使用 boost::make_shared<T>(...) 时遇到 g++4.6 和 boost 1.42 的编译错误, 而 shared_ptr<T>(new T(...))编译就好了。不幸的是,我无法隔离一个最小的示例(我尝试为两者编译的任何内容都很好),但也许有人可以向我解释其中的区别。

我正在实例化 shared_ptr<ResidualsFunctor> f 的实例,其中 ResidualsFunctor有以下 ctor:

ResidualsFunctor(int,int,StaticEquilibriumSolver*)

这个
f=shared_ptr<ResidualsFunctor>(new ResidualsFunctor(0,0,this)); // this is a StaticEquilibriumSolver*

编译得很好,而
f=make_shared<ResidualsFunctor>(0,0,this); 

告诉我:
/usr/include/boost/smart_ptr/make_shared.hpp: In function 'boost::shared_ptr<T> boost::make_shared(Args&& ...) [with T = StaticEquilibriumSolver::ResidualsFunctor, Args = int, int, StaticEquilibriumSolver* const]':
pkg/sparc/SparcField.cpp:472:49:   instantiated from here
/usr/include/boost/smart_ptr/make_shared.hpp:148:5: error: no matching function for call to 'forward(int&)'
/usr/include/boost/smart_ptr/make_shared.hpp:148:5: note: candidate is:
/usr/include/boost/smart_ptr/make_shared.hpp:90:40: note: template<class T> T&& boost::detail::forward(T&&)

是boost的bug吗?在 gcc 中?我的错,我没有看到?

最佳答案

boost::make_shared使用给定的参数分配给定类型的对象并将其包装在 boost::shared_ptr 中.因此,它必须将您提供给它的参数转发给构造函数。为了使您对它的调用起作用,它必须能够找到与您提供的参数列表相匹配的构造函数。

您的问题似乎是很难转发您的整数参数。我不确定如何,因为您的所有参数都是基本类型。

Boost 1.42 发布于 18 个月前; GCC 4.6 的发布时间比这更晚。我想如果你更新到更新版本的 Boost,你就不会遇到这个问题。

关于boost::make_shared<T>(...) 不编译, shared_ptr<T>(new T(...)) 编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6895980/

相关文章:

c++ - 如何使用 boost::adaptors 链的 boost 范围获取 std::list

c++ - 需要对 Boost asio 异步操作和计时器进行一些说明

c++ - 在条件变量上调用 notify 时互斥量是否被解锁?

c++ - std::find 中的 Lambda 问题

c++ - 如何在 C++ 中将对来自一个 vector 的对象的引用存储在另一个 vector 中?

c++ - 将 "using namespace boost::numeric::ublas;"与 STL vector 一起使用时出错

c++ - 创建虚拟共享对象 (.so) 以依赖于其他共享对象

c++ - 使用 STL 的图形(列表 vector ,即邻接列表)- C++

c++ - 为什么我的程序在 boost::enable_shared_from_this<>/boost::shared_ptr<> 中崩溃?

c++11 - 用 std::unique_ptr/std::shared_ptr 确认线程安全