Boost logo

Boost :

Subject: [boost] [local_function] N-papers, use cases, etc
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2010-09-11 12:41:02


Hello all,

Do you know of any N-paper, use case, example, etc I should consider
for local functions?

Now I have a basic implementation of Boost.LocalFunction so I am
looking for examples to implement to test if the library supports all
necessary functionalities and use cases.

1) I have found this N2511 (and I will also check the N-papers it cites):
http://www.open-std.org/JTC1/sc22/wg21/docs/papers/2008/n2511.html

2) My original use case for local functions was a constant code block ("cb"):

class c {
    void f(int x) {
        const int y = x;

        {
            BOOST_LOCAL_FUNCTION(
            (void) (cb0_code)( (const bound)((this)(&x)(&y)) )
            ) { // Code compiled in const-correct context.
                assert(x == y);
                assert(x > 0);
            } BOOST_LOCAL_FUNCTION_END(cb0_code)
            cb0_code(); // Execute code now.
        }
    }
};

3) Also scope exit ("se") can be implemented using local functions and
therefore considered as a use cases for local functions:

class c {
    void f(int x) {
        const int y = x;

        BOOST_LOCAL_FUNCTION(
        (void) (se0_code)( (auto bound)((this)(&x)(&y)) )
        ) {
            ... // Stuff to do at scope exit.
        } BOOST_LOCAL_FUNCTION_END(se0_code)
        // Destructor executes code when this object goes out of scope.
        struct se0_resource {
            void ~se0_resource() { se0_code(); }
        } se0_object;
    }
};

Where `auto bound` binds the variables in scope keeping their original
const-qualifiers (e.g., `y` is bound as `const&` while `x` is bound as
non-const `&`). Instead, `const bound` forces a const bound and
`bound` forces a non-cost bound (e.g., using `(bound)((&y))` would
have given a compile-time error because `y` is of const type so it
cannot be bound as non-const, while using `(const bound)((&x))` would
have been OK because any type can be promoted to be const as usual in
C++).

--
Lorenzo

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