boost::make_shared(...) 无法编译,shared_ptr(new T(...)) 可以编译

发布于 2024-11-27 09:42:17 字数 1311 浏览 1 评论 0原文

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

我正在实例化 shared_ptr的实例; f,其中 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法回应 2024-12-04 09:42:17

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 a boost::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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文