Copy a struct and its context

Yuxuan Shui via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 13 14:01:26 PDT 2016


On Tuesday, 13 September 2016 at 20:36:22 UTC, Steven 
Schveighoffer wrote:
> On 9/13/16 4:11 PM, Yuxuan Shui wrote:
>> On Tuesday, 13 September 2016 at 20:00:40 UTC, Steven 
>> Schveighoffer wrote:
>>> Not familiar with C++ lambda. You can always "specify" how to 
>>> capture
>>> the data by directly declaring it:
>>>
>>> auto foo()
>>> {
>>>     int x;
>>>     static struct S
>>>     {
>>>         int x;
>>>     }
>>>     return S(x);
>>> }
>>
>> It just feels a bit tedious to do something manually while the 
>> compiler
>> have enough information to do it for me.
>
> Do what for you? How does it know that you don't want to use a 
> closure and a reference to that instead?
>
> Note that all the internals for this are implementation 
> defined. Given sufficient conditions, the compiler could 
> "cheat" and allocate the data inside the struct itself instead. 
> For example, if all referenced data was immutable.
>
> -Steve

For example, a common use case might be I want to capture 
everything by value. In stead of adding all the fields by hand 
and passing them to the constructor, I want the compiler to do it 
for me.

i.e. I wish I could (borrowing C++ syntax):

struct A[=] {
    ...
}

Then the context will be captured by value instead of reference.


More information about the Digitalmars-d-learn mailing list