boost::make_shared<T>(...) does not compile, shared_ptr<T>(new T(...)) does
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 开发者_Go百科= 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?
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.
精彩评论