Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #81188

Re: Does static initialization of a local variable always happens synchronized ?

From Bonita Montero <Bonita.Montero@gmail.com>
Newsgroups comp.lang.c++
Subject Re: Does static initialization of a local variable always happens synchronized ?
Date 2021-09-12 08:24 +0200
Organization A noiseless patient Spider
Message-ID <shk6eb$dm0$1@dont-email.me> (permalink)
References <shhrig$7fl$1@dont-email.me> <shivfh$98b$1@dont-email.me> <shj25j$j54$1@dont-email.me>

Show all headers | View raw


Am 11.09.2021 um 22:05 schrieb Alf P. Steinbach:
> On 11 Sep 2021 21:19, Bonita Montero wrote:
>> Something I wondered about is whether it is possible to get a C function
>> pointer from a local static lambda _with_ captures like this:
>>
>> #include <iostream>
>>
>> int main()
>> {
>>      int i;
>>      static
>>      auto lambda = [&i]()
>>      {
>>          ++i;
>>      };
>>      void (*pLambda)() = lambda;
>> }
> 
> This particular example — the lines of code up to the hypothetical 
> declaration of `pLambda` — is flawed because if you put this code in
> any function other than `main` it will then use a dangling reference
> on second call of that function.

Of course there could be a dangling reference, but that's not what I'm
discussing.


> ---
> 
>> In theory this should be possible since the lambda doesn't need
>> its own context-pointer (pseudo-"this") since the capture-context
>> is global storage.

> If you make `i` static then you have something that a smart enough 
> compiler can rewrite to a capture-less lambda. Don't know if they do 
> though. And conversion to C function pointer would need to be a language 
> extension.

Of course I could make i static, but that's not what I'm talking about.

Back to comp.lang.c++ | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Does static initialization of a local variable always happens synchronized ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-11 11:06 +0200
  Re: Does static initialization of a local variable always happens synchronized ? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2021-09-11 13:47 +0200
    Re: Does static initialization of a local variable always happens synchronized ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-11 16:17 +0200
      Re: Does static initialization of a local variable always happens synchronized ? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2021-09-11 17:09 +0200
        Re: Does static initialization of a local variable always happens synchronized ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-11 17:18 +0200
        Re: Does static initialization of a local variable always happens synchronized ? David Brown <david.brown@hesbynett.no> - 2021-09-11 20:03 +0200
  Re: Does static initialization of a local variable always happens synchronized ? Paavo Helde <myfirstname@osa.pri.ee> - 2021-09-11 15:13 +0300
  Re: Does static initialization of a local variable always happens synchronized ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-11 16:02 +0200
  Re: Does static initialization of a local variable always happens synchronized ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-11 21:19 +0200
    Re: Does static initialization of a local variable always happens synchronized ? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2021-09-11 22:05 +0200
      Re: Does static initialization of a local variable always happens synchronized ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-12 08:24 +0200

csiph-web