Relocatable objects and internal pointers

Matt Elkins via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jan 29 17:07:28 PST 2016


Hi all, I'm a C++ programmer trying to decide whether to switch 
my main focus to D, and so I'm working on a pet project using it. 
So far I really like some of the practical aspects of the 
language (built-in contracts are great, the metaprogramming is 
very accessible, and I can't enough of these compile speeds!), 
but I keep finding myself frustrated by what seem like 
expressiveness limitations (unless, as I hope, they are just 
examples of my newbieness shining through). Case in point:

In an attempt to work around one apparent limitation (previously 
asked about here 
http://forum.dlang.org/thread/eizmagtimvetoganawrr@forum.dlang.org) I came up with an idea which would require storing internal points in a struct. A very stripped-down but illustrative example would be something like this:

[code]
struct Foo
{
     invariant
     {
         assert(m_this == &this);
     }

     @disable(this);

     this(/* arguments to populate stuff */)
     {
         m_this = &this;
         /* ... populate stuff ... */
     }

     this(this)
     {
         m_this = &this;
         /* ... do more stuff ... */
     }

     private:
         Foo* m_this;
         /* ... stuff ... */
}
[/code]

This is just a piece of what I am doing, if you are wondering why 
I am bothering to save a pointer to this. However, I was doing 
some reading on D and came across a section in TDPL which said 
internal pointers are verboten because objects must be 
relocatable. Does this mean my example is invalid (e.g., the 
invariant will not hold in all circumstances)? If it is invalid, 
does that mean there are circumstances under which the post-blit 
constructor can be elided when performing a copy or copy-like 
operation (such as a move)? I've been treating it like a sort of 
copy-constructor that lacks visibility on the copied-from object, 
but maybe that's a mistake...


More information about the Digitalmars-d-learn mailing list