Opened 12 years ago
Last modified 12 years ago
#4646 new Feature Requests
[function][patch] adding an unchecked_invoke method to function objects
Reported by: | Owned by: | Douglas Gregor | |
---|---|---|---|
Milestone: | To Be Determined | Component: | function |
Version: | Boost 1.44.0 | Severity: | Problem |
Keywords: | function unchecked | Cc: |
Description
A common idiom for using boost::function objects is as follows :
boost::function<...> func1; ... if( !func1.empty() ) { func1(); }
Internally to boost::function::operator()(...) there is then a subsequent call to empty(), which if fails calls a boost::throw_exception.
In developing a library that makes use of boost::function, and calling the contained function I am forced to define boost::throw_exception, OR require my users to do so. The library in question is for an embedded platform (without exception support), and so I have also defined BOOST_NO_EXCEPTION and BOOST_EXCEPTION_DISABLE, it therefore becomes imperative that I check the function is valid prior to calling it, because I have no exception support. The current implementation of boost::function however does not allow me to work around this call, and requres me to defnie a function that will never be called, and conflict with my library users should they wish to define an alternative boost::throw_exception for the aspects of boost they may use.
I am proposing the addition of unchecked_invoke(...) that allows users of boost::function to call a function object without this checking on every call. This will not only allow my scenario to work, but also allow the common test before use to benefit, especially in the case where a function object is called within a loop.
As reference this topic was discussed in this thread :
http://lists.boost.org/Archives/boost/2010/09/170486.php
and the proposed patch is for function_template.hpp :
Insert around line 767
#if BOOST_WORKAROUND(BOOST_MSVC, < 1300) // MSVC 6.0 and prior require all definitions to be inline, but // these definitions can become very costly. result_type unchecked_invoke(BOOST_FUNCTION_PARMS) const { return get_vtable()->invoker (this->functor BOOST_FUNCTION_COMMA BOOST_FUNCTION_ARGS); } #else result_type unchecked_invoke(BOOST_FUNCTION_PARMS) const; #endif
and later around line 1015
#if !BOOST_WORKAROUND(BOOST_MSVC, < 1300) template<typename R BOOST_FUNCTION_COMMA BOOST_FUNCTION_TEMPLATE_PARMS> typename BOOST_FUNCTION_FUNCTION< R BOOST_FUNCTION_COMMA BOOST_FUNCTION_TEMPLATE_ARGS>::result_type inline BOOST_FUNCTION_FUNCTION<R BOOST_FUNCTION_COMMA BOOST_FUNCTION_TEMPLATE_ARGS> ::unchecked_invoke(BOOST_FUNCTION_PARMS) const { return get_vtable()->invoker (this->functor BOOST_FUNCTION_COMMA BOOST_FUNCTION_ARGS); } #endif
}}}
Feature request #4720 addresses the same issue in a different way.