I'm trying to convert some code to C++11 on gcc 4.7.2. Part of the code included the assignment of a boost::factory
to a boost::function
. However, the compilation fails with lots of warnings if I change it to std::function
. Here's sample code the reproduces the error:
#include <boost/functional/factory.hpp>
#include <boost/function.hpp>
#include <functional>
struct S {
S(int) {}
};
int main() {
boost::function<S*(int)> b = boost::factory<S*>(); // ok
std::function<S*(int)> s = boost::factory<S*>(); // error
}
This gives error: no match for call to ‘(boost::factory<S*>) (int)’
Interestingly, if S
takes no arguments, std::function<S*()>
assigns to the boost::factory
without complaint. Why doesn't this work? Is there a work around to allow assignment of a boost::factory
to an std::function
? I'm using Boost.1.53
It seems that boost::factory<S*>
insists on its argument to be bound to a reference:
boost::factory<S*> factory;
factory(17); // error
int argument(17);
factory(argument); // OK
Based on this, it seems that boost::function<S*(int)>
passes the argument as int
without perfectly forwarding it while std::function<S*(int)>
seems to use perfect forwarding on the argument. As a result, the argument cannot be bound to boost::factory<S*>
argument.
When using gcc's std::function<...>
passing the argument by const&
works:
std::function<S*(int const&)> factory = boost::factory<S*>();
Unfortunately, the same work-around does not work with the std::function<...>
from libc++. Since you said that you tried to compile the code with gcc that may not be much of an issue, though.