Boost logo

Boost :

Subject: [boost] [local_function] any interest in a LocalFunction library?
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2010-08-22 11:09:56


Hello all,

Is there interest in a library that implement local functions for C++?

Boost.LocalFunction (PROPOSAL DRAFT)

This library allows to define and invoke a function locally within a code block.
In addition to the parameters that can be passed to the function as
usual upon its invocation, any variable within scope can be /binded/
to the local function declaration and used by the local function
definition code.

MOTIVATING EXAMPLE

Consider the following example:

    #include <boost/local_function.hpp>

    class c {
    public:
        c(double const& x): x_(x) {}

        void f(double& x) {
            double y = x * 10.0;
            bool r;
            {
                double z = x * 100.0;

                // Local function definition.
                BOOST_LOCAL_FUNCTION( (bool) (l)(
                    (char* const)(a) // Usual non-binded param.
                    (bool)(b) // Another usual non-binded param.
                    (binded)(
                        (&x) // Binded param by reference.
                        (const& x_) // Binded param by const reference.
                        (y) // Binded param by value (copy).
                        (const z) // Binded param by const value (copy)
                        (const this) // Binded const object (by pointer).
                // (this) // Or, binded non-const object.
                    )
                ) ) {
                    // Use `this_` here (not `this`) to access binded object.
                    assert( this_->eq(x_) );
                    ... // Local function definition code.
                } BOOST_LOCAL_FUNCTION_END

                // Local function call only specifies non-binded params.
                bool r1 = BOOST_LOCAL_FUNCTION_CALL(l)("hello", true);
                bool r2 = BOOST_LOCAL_FUNCTION_CALL(l)("bye", false);

                r = r1 || r2;
            } // Local function goes out of scope here.
            ...
        }

        bool eq(double const& x) const { return x_ == x; }

    private:
        double x_;
    };

Note the following:

1) The local function `l` is only defined locally within a code block
of the function `f`.

2) The parameter `x` of the function `f`, the member variable `x_`,
the local variables `y` and `z`, and the object `this` are binded to
the declaration of the local function.
Therefore, they can all be used by the local function definition code
even if they are not explicitly passed to the local function when it
is invoked.
(Also note that the binding syntax conveniently does not require the
user to specify the types of the binded parameters but just their
/binding-qualified/ names.)

3) The local function can be invoked (multiple times) from within the
scope of its declaration.
As usual, each invocation specifies the non-binded parameters and, for
non-void functions, it returns the function result value.

BACKGROUND

I have not yet implemented Boost.LocalFunction. I will be implementing
local-function features as part of Boost.Contract development (in
order to support constant-correct block invariants and loop variants).
I am probing for interest to understand how much effort I shall invest
in making the local-function features a standalone library like
Boost.LocalFunction separated from Boost.Contract.

On Thu, Apr 15, 2010 at 7:02 PM, Alexander Nasonov <alnsn_at_[hidden]> wrote:
> vicente.botet wrote:
>> VBE> Boost.ScopeExit showed a technique that can be used to
>> emulate local functions. Alexander proposed long time ago to provide
>> it independently from ScopeExit but to my knowledge this has not
>> been done.
>
> Sorry for the late reply, I'm not following every boost thread, I got
> this message via google alerts.
>
> Yes, this has not been done. Local function can borrow argument
> binding syntax from ScopeExit but there are other things I need to think
> of. For instance, in
>
> int BOOST_LOCAL_FUNCTION(f,
>    (BOOST_LOCAL_FUNCTION_BIND( (a)(&b) ), int c)) {
>    // ...
> } BOOST_LOCAL_FUNCTION_END
>
> syntax, is there any way to shorten BOOST_LOCAL_FUNCTION_BIND to "bind"?
>
> The syntax doesn't have to be exactly as above. If someone could
> take a lead on this it would be great. I don't have time for this,
> I'm afraid.

The Boost.LocalFunction implementation will largely rely on parameter
binding techniques similar to the ones currently used by
Boost.ScopeExit as suggested to me by Steven Watanable, Vicente Botet,
and Alexander Nasonov.

Thank you.

-- 
Lorenzo

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk