Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

unfused_generic

Description

An n-ary Polymorphic Function Object adapter template for an unary Polymorphic Function Object target function. When called, its arguments are bundled to a Random Access Sequence of references that is passed to the target function. Non-const LValue arguments are transported as references to non-const, otherwise references to const are used.

[Tip] Tip

Detecting mutable LValues on a per-argument basis is currently a compile time expensive operation (see The Forwarding Problem for details). Therefore, there are two, lightweight and more restricted variants of this class template, unfused_lvalue_args and unfused_rvalue_args.

The overload set of the adapter's function call operator can be restricted by removing the type member from the nested result metafunction of the Polymorphic Function Object (in this case the substitution-failure-is-not-an-error principle applies for non-nullary case and nullary calls are explicitly disabled by the library).

[Caution] Caution

As the nullary call operator cannot be a template it will be instantiated along with the class template, so it must be disabled (as described above) in cases where it isn't instantiable.

The type of the target function is allowed to be const qualified or a reference. Const qualification is preserved and propagated appropriately (in other words, only const versions of operator() can be used if the target function object is const - or, in case the target function object is held by value, the adapter is const).

Header
#include <boost/fusion/functional/adapter/unfused_generic.hpp>
Synopsis
template <class Function>
class unfused_generic;
Template parameters
Parameter Description Default
Function An unary Polymorphic Function Object  
Model of

Notation

F
A possibly const qualified, unary Polymorphic Function Object type or reference type thereof
f
An object convertible to F
UG
The type unfused_generic<F>
ug
An instance of UG, initialized with f
a0...aN
Arguments to ug
Expression Semantics
Expression Semantics
UG(f) Creates a fused function as described above, initializes the target function with f.
UG() Creates a fused function as described above, attempts to use F's default constructor.
ug(a0...aN) Calls f with a Sequence that contains references to the arguments a0...aN.
Example
template <typename Function, typename T>
class fused_bound_1st
{
    typename traits::deduce<Function>::type fnc_deferred;
    typename traits::deduce<T>::type        xxx_bound;
public:

    fused_bound_1st(Function deferred, T bound)
        : fnc_deferred(deferred), xxx_bound(bound)
    { }

    template <class Seq>
    struct result
        : result_of::invoke< Function,
            typename result_of::push_front<Seq, T>::type >
    { };

    template <class Seq>
    typename result<Seq>::type operator()(Seq const & s) const
    {
        return invoke(fnc_deferred, push_front(s,xxx_bound));
    }
};

template <typename Function, typename T>
unfused_generic< fused_bound_1st<Function,T> > 
bind_1st(Function f, T const & x)
{
    return unfused_generic< fused_bound_1st<Function,T> >(
        fused_bound_1st<Function,T>(f,x) ); 
}

int test_func(int a, int b, int c)
{
    return a+b+c;
}

void try_it()
{
    assert(bind_1st(& test_func,3)(-2,-1) == 0);
    assert(bind_1st(std::plus<float>(), 1)(0.5f) == 1.5f);
}
See also
Copyright © 2001-2005 Joel de Guzman, Dan Marsden

PrevUpHomeNext