[Dwarf-discuss] Representing captured `this` in C++ lambdas

Kyle Huey khuey@pernos.co
Wed Apr 23 23:42:43 GMT 2025


On Wed, Apr 23, 2025 at 3:14 PM David Blaikie <dblaikie@gmail.com> wrote:
>
>
>
> On Wed, Apr 23, 2025 at 2:53 PM Kyle Huey <khuey@pernos.co> wrote:
>>
>> On Wed, Apr 23, 2025 at 2:20 PM David Blaikie <dblaikie@gmail.com> wrote:
>> >>
>> >> Do you object to anything I proposed other than removing the
>> >> representation of the anonymous class compilers generate for lambdas?
>> >
>> >
>> > I'm not a /super/ fan of introducing a bunch of locals in addition to the member descriptions - it'll be a bunch of extra DWARF that'd be nice to avoid if we can...
>>
>> Yeah, that would be the reason to get rid of the representation of the
>> anonymous struct. Then we're just converting members into locals
>> rather than duplicating anything. Even if you really want to keep the
>> class itself, the members could be dropped.
>
>
> Except it's likely users will want to inspect the state of a lambda in some situations. They get passed around, stored (in std::functions or similar type-erased things, often), etc in many cases & may be important to know what they represent when not near/in a call to the lambda. So having the members described seems important.

Mmm, yes, ok.

- Kyle

>>
>> > But putting object_pointer on the class member that stores "this" seems problematic since that's effectively at the same scope as the real object pointer - it'd be awkward to say there's two "this" at the same scope and have to say that the member variable "this" shadows the real "this" in some way.
>> >
>> > And then you want the captured variables to be in a scope that is inside the "this" scope so they override unqualified lookup for any names that are also members of "this"...
>> >
>> > So, yeah, I get why you/gcc developers arrived where they did. I wouldn't mind some size analysis to see how bad the regression/cost is, that might help inform whether it's worth trying to address it.
>>
>> Hmm. I could look at hacking something up to measure but it's not at
>> the top of my priority list.
>
>
> Fair.


More information about the Dwarf-discuss mailing list