我在使用 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/