pass-by-ref semantics for structs (was Deque impl.)

Steven Schveighoffer schveiguy at yahoo.com
Thu Jan 31 08:00:15 PST 2013


On Thu, 31 Jan 2013 10:40:15 -0500, Andrei Alexandrescu  
<SeeWebsiteForEmail at erdani.org> wrote:

> On 1/31/13 10:29 AM, Steven Schveighoffer wrote:
>> On Thu, 31 Jan 2013 10:21:04 -0500, Andrei Alexandrescu
>> <SeeWebsiteForEmail at erdani.org> wrote:
>>
>>> On 1/31/13 10:18 AM, Steven Schveighoffer wrote:
>>>> On Thu, 31 Jan 2013 10:12:53 -0500, Andrei Alexandrescu
>>>>> As far as I can tell classes have the same problem.
>>>>
>>>> Nope.
>>>>
>>>> void foo(someclass aa, int x, int y)
>>>> {
>>>> aa[x] = y;
>>>> }
>>>>
>>>> void main()
>>>> {
>>>> someclass aa;
>>>> foo(aa, 1, 2); // segfault
>>>> ...
>>>> }
>>>
>>> We could easily arrange things to segfault just the same with a
>>> struct-based implementation.
>>
>> So you want to make a struct that acts just like a class? I'm not seeing
>> the point.
>
> It has a destructor.

One which is not called if allocated on the heap.

It's possible to make a class reference that is destroyed when going out  
of scope.  Then you have the option, heap destroyed or stack destroyed.

Not possible with structs (at least for now).

-Steve


More information about the Digitalmars-d mailing list