At the moment, it's not possible to compose std::function on top of
boost::any because you can't get the interface back out of boost::any. But
with a polymorphic_any_cast, you can. Consider the following:
template<typename Signature> class function;
template class function {
boost::any contents;
struct callable {
virtual Ret call(Args... args) = 0;
};
template<typename F> struct derived : callable {
F f;
derived(F obj) : f(std::move(obj)) {}
derived(derived&&) = default;
derived(const derived&) = default;
derived& operator=(derived&&) = default;
derived& operator=(const derived&) = default;
Ret call(Args... args) { return f(std::forward<Args>(args)...); }
};
public:
template<typename F> function(F f, /* constraint */ = 0)
: contents(derived<F>(std::move(f))) {}
function(const function&) = default;
function(function&&) = default;
function& operator=(const function&) = default;
function& operator=(function&&) = default;
explicit operator bool() const { return contents; }
Ret operator()(Args... args) {
if (auto callable =
boost::polymorphic_any_cast(&contents))
return callable->call(std::forward<Args>(args)...);
throw std::bad_function_call();
}
};
Here, pretty much all of, well, everything, is just delegated to
boost::any. This isn't the entire interface, but enough that you should be
able to get the idea.
Even on an Itanium ABI system where boost::polymorphic_any_cast can become
a dynamic_cast or you assume future Standard support, this won't be as
efficient as a specialized std::function implementation, or one based on
the more appropriate value_ptr<T>. But the interface offered by
polymorphic_any_cast is substantially more flexible than is required here.
On 28 November 2013 13:36, Antony Polukhin wrote:
2013/11/28 Alexander Nasonov
I'm not *the official* maintainer of the Boost.Any library, I'm just
Antony Polukhin wrote:
the
one who made all the latest changes with rvalues and C++11 support. But
if
there'll be no objections and the official maintainer won't appear I
can
add this functionality to Boost.Any.
If I'm still the official maintainer I can happily pass my maintainership
to Antony.
Thanks! I'll do my best.
--
Best regards,
Antony Polukhin
_______________________________________________
Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost