Relocatable objects and internal pointers

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jan 29 19:00:11 PST 2016


On 1/29/16 9:35 PM, Matt Elkins wrote:
> On Saturday, 30 January 2016 at 02:09:55 UTC, Steven Schveighoffer wrote:
>> I figured out a way to have them. You just have to guarantee you don't
>> copy the actual "pointer" out of the struct:
>>
>> https://forum.dlang.org/post/mk5k4l$s5r$1@digitalmars.com
>
> Unfortunately, that won't work for what I was trying to do. The stuff I
> elided in the comments were more pointers to other Foo instances, used
> to create a linked-list (of stack-allocated objects); these would still
> break under the conditions Ali described. I was only storing the this
> pointer so that blitted objects could deduce where they came from
> (trying to turn the post-blit constructor into a copy-constructor).

Ah, so you were actually counting on the postblit to have an *invalid* 
pointer to begin with :) Yeah, that isn't going to work. In D, it's 
legal to do something like memcpy struct data (with no postblit), and 
this is done quite often in many places because of that.

> Thanks, though. I'm thinking that maybe D just can't express these
> semantics without substantial overhead. While somewhat disappointing (I
> came into D with stars in my eyes :)), it's not enough by itself to make
> me go back to C++, at least not just yet. Not when I can just use a few
> static ifs to do what previously required careful template crafting that
> I wouldn't understand 3 months later. On the other hand, I'm falling
> behind on my library books since I no longer have any time for reading
> during compilations ;).

There are some really smart people who frequent these forums, if you 
post your actual use case, you may get an answer that you hadn't thought of.

I saw you were trying to implement something like std::unique_ptr? There 
is http://dlang.org/phobos/std_typecons.html#.Unique

Not sure if it helps.

-Steve


More information about the Digitalmars-d-learn mailing list