Boost logo

Boost :

Subject: Re: [boost] [local] Help for the Alternatives section
From: lcaminiti (lorcaminiti_at_[hidden])
Date: 2011-05-12 16:16:25


Jeffrey Lee Hellrung, Jr.-2 wrote:
>
> I meant to explicitly ask whether the following are equivalent:
>
> bind(T)& x
> bind(T&) x
>
> [...]
>

No, they are not.

bind(T)& x binds by reference so a change to x within the local scope will
reflect to x in the enclosing scope.
bind(T&) x binds by value so a change to x within the local scope will /not/
reflect to x in the enclosing scope.

int x = 0;
int& r = x;
int y = 0;
int& s = y;

BOOST_LOCAL_BLOCK(bind(int&) r, bind(int&)& s) { // Only s bind by ref.
    r = -1; // Does not affect enclosing scope.
    s = -1; // Affects enclosing scope.
} BOOST_LOCAL_BLOCK_END

assert(r == 0); // Not changed by local block.
assert(s == -1); // Changed by local block.

In bind(T&)& the outer reference is the one that applies to the variable
binding. The inner reference only indicates what the original variable type
is.

--Lorenzo

--
View this message in context: http://boost.2283326.n4.nabble.com/local-Help-for-the-Alternatives-section-tp3408469p3518572.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

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