boost::make_shared(...) 无法编译,shared_ptr(new T(...)) 可以编译
使用 boost::make_shared
时,我在 g++4.6 和 boost 1.42 中遇到编译错误,而 shared_ptr
编译得很好。不幸的是,我无法隔离一个最小的例子(我尝试编译的任何东西都很好),但也许有人可以向我解释其中的区别。
我正在实例化 shared_ptr
,其中 ResidualsFunctor
具有以下构造函数:
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 中的错误吗?在海湾合作委员会?我的错,我没看到吗?
I am getting compilation error with g++4.6 and boost 1.42 when using boost::make_shared<T>(...)
, whereas shared_ptr<T>(new T(...))
compiles just fine. I am unfortunately not able to isolate a minimal example (anything I tried compiled just fine for both), but perhaps someone could explain to me the difference.
I am instatiating an instance of shared_ptr<ResidualsFunctor> f
, where ResidualsFunctor
has the following ctor:
ResidualsFunctor(int,int,StaticEquilibriumSolver*)
This
f=shared_ptr<ResidualsFunctor>(new ResidualsFunctor(0,0,this)); // this is a StaticEquilibriumSolver*
compiles just fine, whereas
f=make_shared<ResidualsFunctor>(0,0,this);
tells me:
/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&&)
Is it a bug in boost? In gcc? My fault which I don't see?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
boost::make_shared
使用给定参数分配给定类型的对象,并将其包装在boost::shared_ptr
中。因此,它必须将您提供给它的参数转发给构造函数。为了使您对它的调用能够工作,它必须能够找到与您提供的参数列表相匹配的构造函数。您的问题似乎是转发整数参数时遇到困难。我不知道如何,因为你所有的论点都是基本类型。
Boost 1.42 于 18 个月前发布; GCC 4.6 的发布时间比这要晚。我猜如果你更新到更新版本的Boost,就不会出现这个问题了。
boost::make_shared
allocates an object of the given type, using the given parameters and wraps it in aboost::shared_ptr
. Therefore, it has to forward the arguments you give it to a constructor. In order for your call to it to work, it must be able to find a constructor that matches the argument list you give it.Your problem seems to be that it is having difficulty forwarding your integer arguments. I'm not sure how, as all of your arguments are basic types.
Boost 1.42 was released 18 months ago; GCC 4.6 was released rather more recently than that. I'd guess that if you update to a more recent version of Boost, you wouldn't have this problem.