C++17 cannot beat D surely

Stanislav Blinov via Digitalmars-d digitalmars-d at puremagic.com
Tue Jun 6 20:39:24 PDT 2017


On Wednesday, 7 June 2017 at 03:06:34 UTC, Ali Çehreli wrote:
> On 06/06/2017 06:09 PM, Steven Schveighoffer wrote:
>
> > But it is pretty well known that enum'ing an array can have
> it allocate
> > wherever it is used.
>
> One of the most effective examples is comparing .ptr with 
> (seemingly) itself:
>
> void main() {
>     enum e = [ 1 ];
>     static assert(e.ptr != e.ptr);
>     assert(e.ptr != e.ptr);
> }
>
> Both asserts pass. I'm surprised that e.ptr is usable at 
> compile time. Fine, I guess... :)
>
> Ali

A bit OT, but how about this one?

struct Tree {
     struct Node {
         Node* left, right;
     }

     Node* root = new Node;
}

void main() {
     Tree tree1, tree2;
     assert(tree1.root);
     assert(tree2.root);
     assert(tree1.root != tree2.root); // fails
}

Not `static`, not `enum`, but `new` at compile time and `root` is 
of course statically initialized :)


More information about the Digitalmars-d mailing list