Boost logo

Boost :

Subject: Re: [boost] [local_function] End scope function name declaration makes code hard to read.
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2013-03-15 18:20:32


On Fri, Mar 15, 2013 at 2:33 PM, Mostafa <mostafa_working_away_at_[hidden]> wrote:
> (This is a feature request/inquiry.)
>
> I'm just now seriously starting to use local_function, and I'm finding that
> having to "visually parse" until the end of scope to determine the local
> function name makes the code much harder to read. Is it possible to declare
> the function name at the beginning of the scope? (I don't mind having to
> also register the function name at the end of the scope.) Something like:
>
> BLF2(add, (int), (int lhs, int rhs))
> {
> return lhs + rhs;
> } BLF2_REGISTER(add);

To repeat the name is a bad idea because you duplicate that
information. Just use a code comment after the result type to do that:

    void /* add */ BOOST_LOCAL_FUNCTION(const bind factor, bind& sum, int num) {
        sum += factor * num;
    } BOOST_LOCAL_FUNCTION_NAME(add)

> Having said, I don't see why needing an "ending macro" for the local
> function scope is a requirement

[5] Rationale. The local function name must be passed to the macro
BOOST_LOCAL_FUNCTION_NAME ending the function definition so this macro
can declare a local variable with the local function name to hold the
local function object. Therefore the local function name cannot be
specified within the BOOST_LOCAL_FUNCTION and it must appear instead
after the local function body (even if that differs from the usual C++
function declaration syntax).

http://www.boost.org/doc/libs/1_53_0/libs/local_function/doc/html/boost_localfunction/tutorial.html#ftn.boost_localfunction.tutorial.local_functions.f0

> (albeit my understanding of the library
> internals is limited to implementation annex of the documentation). Can't
> the start macro just expand to something like:

Unfortunately not :( The Implementation section is just a very
simplified example of what the macros generate:

Note
The code listed here can be used by curious readers and library
maintainers as a reference in trying to understand the library source
code. There is absolutely no guarantee that the library implementation
uses the exact code listed here.

Look at the source code, there are a couple of code comments that
mention "here's why the name is needed here" but you have to dig into
the code...

> //-----------------------------
> //Macro Generated Output Start.
> //-----------------------------
> //Local class definition.
> //Local class instance registration.
> //Out of class method definition starts here:
> inline void local_add::body(int& sum, const int& factor, const int& num)
> //-----------------------------
> //Macro Generated Output End.
> //-----------------------------
>
> //User generated content starts here:
> {
> return lhs + rhs;
> }
>
> Thanks,
>
> Mostafa
>
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost

HTH,
--Lorenzo


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