Boost logo

Boost :

Subject: Re: [boost] [local_function] any interest in a LocalFunction library?
From: John Bytheway (jbytheway+boost_at_[hidden])
Date: 2010-09-15 18:11:36


On 15/09/10 03:40, Lorenzo Caminiti wrote:
> On Tue, Sep 14, 2010 at 2:25 PM, John Bytheway
>> BOOST_LOCAL_FUNCTION(
>> (Foo) (unamed)( (bound)((&x)) )
>> ) {
>> Foo y;
>> y.setMagic(x);
>> return y;
>> } BOOST_LOCAL_FUNCTION_END(unamed)
>> Foo const y = unamed();
>>
>> but is there value in providing dedicated functionality? e.g. there are
>> three occurrences of 'Foo' there which might in theory by named only once.
>
> If you could pick, what would be your preferred syntax in this case?

Well, with C++0x lambdas it looks like (roughly, I haven't learnt this
syntax properly yet):

Foo const y = [&x]() { Foo y; y.setMagic(x); return y; }

and it seems like we could come pretty close to that. Mimicking your
existing syntax fairly closely would lead to:

BOOST_INITIALIZE_FROM_FUNCTION(
  (Foo const)(y)((bound)((&x)))({ Foo y; y.setMagic(x); return y; })
)

but it occurs to me that these sort of functions would never need
arguments (since you can't supply them), so I guess we could abbreviate
it slightly, maybe to

BOOST_INITIALIZE_FROM_FUNCTION(
  (Foo const)(y)((&x))({ Foo y; y.setMagic(x); return y; })
)

So, four things passed to the macro:
- Type of declared variable.
- Name of declared variable.
- PP_SEQ of bindings (could make this optional).
- Function body.

Arguably the braces could be omitted too, but I think it's clearer to
leave them.

The macro will have to remove_const to deduce the return type for the
local function. That makes me wonder whether you would also want to
remove_reference, but it is not clear to me; I can't think of any use
cases either way.

>> BOOST_LOCAL_FUNCTION(
>> (Foo) (unamed)( (bound)((&x)) )
>> ) {
>> Bar const w(x);
>> return Foo(w);
>> } BOOST_LOCAL_FUNCTION_END(unamed)
>> Foo y = unamed();
>>
>> and again it might be helpful to support this use case directly.
>
> Again, if you could pick, what would be your preferred syntax in this case?

I think the exact same syntax works here, which is pleasing:

BOOST_INITIALIZE_FROM_FUNCTION(
  (Foo)(y)((&x))({ Bar const w(x); return Foo(w); })
)

>> I'm not convinced either of these (alone or together) is indeed worth a
>> dedicated syntax, but they're other use cases to consider. In
>> particular, these are cases where the name of the function doesn't
>> really matter (and would be troublesome to settle on -- I'd probably use
>> "make_y" or something similarly banal).
>
> Thanks for the examples. I am not sure I can provide a dedicated
> syntax due to the presence of `type var-name = ...` _after_ the local
> function declaration macros but if you (as well as other Boosters)
> propose an ideal macro syntax, I can see if I can implement something
> like that.

Well, I think the above is quite neat; apart from the massive macro name
it's essentially as brief as using a lambda. Any other opinions?

John Bytheway


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