Boost logo

Boost :

Subject: Re: [boost] [lclcontext] Any interest in Pythonesque "with-as" statement in C++.
From: Mostafa (mostafa_working_away_at_[hidden])
Date: 2015-08-17 19:37:00


On Mon, 17 Aug 2015 09:48:55 -0700, Sam Kellett <samkellett_at_[hidden]>
wrote:

>> I didn't know that. I was only targeting C++03/C++11.
>>
>
> This should work with C++11:
>
> template <typename T, typename F>
> void with(T &&t, F &&fn)
> {
> fn(std::forward<T>(t));
> }
>
> with(make_foo(), [](foo &&f) {
> // do things with foo.
> }); // foo is destroyed.
>
> Don't think I can help w/r/t C++03 though, the lack of lambdas is a
> killer.
>

One thing this method doesn't do is automatically determine if the scope
exit prematurely.


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