Boost logo

Boost :

Subject: Re: [boost] [local_function] any interest in a LocalFunction library?
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2010-09-16 15:46:35

On Wed, Sep 15, 2010 at 10:56 AM, Lorenzo Caminiti
<lorcaminiti_at_[hidden]> wrote:
> 8) I will not provide the `BOOST_LOCAL_BLOCK` but only the
> `BOOST_LOCAL_CONST`. The only reason for `BOOST_LOCAL_BLOCK` would be
> to break to its exit using `return` and a local function executed
> immediately after it is defined can be used for that purpose.

I have decided to provide a single block macro `BOOST_LOCAL_BLOCK`
which allows to bind both const and non const so the users can decide
which bound variables need to be cons within the block and which do
not. If you want a pure const-block then you'll simply bind all
variable as `const&`. Blocks of course execute in-place right where
they are defined. I have also kept the block break feature via the
macro `BOOST_LOCAL_BLOCK_BREAK` (you get a compiler error if you just
try to use `return`).

For example (this compiles and works):

#include <contract/detail/local/block.hpp>
#include <cassert>

struct c {
    c(): x_(0.0) {}

    void f(int& x) { // Non-const member function so `this` is not const.
        // Non-const `this` but const `x`.
        CONTRACT_DETAIL_LOCAL_BLOCK( (const bind)((&x)) (bind)((this)) ) {
            this_->x_ = x; // Non-const `this` cant be modified.
            assert(x == this_->x_); // Compiler error if `=` instead of `==`.
            CONTRACT_DETAIL_LOCAL_BLOCK_BREAK; // Jump to block's end.
            assert(false); // Never executed.

        x = x_; // Non-const `x` changed outside block.

    int x_;

int main() {
    c cc;
    int x = 1;
    return 0;


Boost list run by bdawes at, gregod at, cpdaniel at, john at