lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lmi] Micro-optimization in ledger_format


From: Greg Chicares
Subject: Re: [lmi] Micro-optimization in ledger_format
Date: Fri, 18 Jan 2019 12:55:15 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.0

On 2019-01-17 01:35, Greg Chicares wrote:
[...]
>     // Initialize an integer to zero.
>     static int zero{[]() {int z {0}; return z;} ()};
> 
> In C++98, I could write that off the top of my head, and feel highly
> confident that I'd gotten it right the first time.

Let me try doing exactly that right now, with a lambda:

  static int zero = [] {int z = 0; return z;} ();

Trying again, a few minutes later, remembering that after writing the
line above off the top of my head, I'd wanted to add 'const':

  static int const zc = [] {int const z{0}; return z;} ();

So I do know how to do this, because it's just a lambda:
  = [] {...}
that's executed immediately:
  = [] {...} ();
Crucially, it's not (as I'd originally thought) some oddball specialized
"IIFE" syntax that must be learned separately in addition to the normal
lambda syntax, so the question is just whether or not to use lambdas
(which we've already answered in the affirmative).

Thus, while this had looked alien to me:

    static std::stringstream interpreter
        {[]
            {
            std::stringstream ss {};
            ss.imbue(blank_is_not_whitespace_locale());
            return ss;
            } ()
        };

this doesn't:

    static std::stringstream interpreter = []
        {
        std::stringstream ss {};
        ss.imbue(blank_is_not_whitespace_locale());
        return ss;
        } ();

and it has the compelling advantage that it makes it harder to
misplace 'static':

https://lists.nongnu.org/archive/html/lmi/2019-01/msg00013.html
| [...] Distracted by incidental syntactic concerns, I hadn't noticed
| that I had the 'static' variable in the wrong place.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]