On Tuesday, 2 July 2013 at 16:59:50 UTC, TommiT wrote:
D
---
struct Wrap(Gift)
{
    Gift gift;
}

struct Teddy
{
    int id;

    Wrap!Teddy getWrapped() @property
    {
        return Wrap!Teddy(this);
    }

    alias getWrapped this;
}

C++ doesn't have alias this. The behavior that it should be simulating is inheritance, I'd probably fail writing C++ for this so here is some D code to translate:

import std.stdio;

class Wrap(Gift) {
        abstract Gift gift();
}

class Teddy : Wrap!Teddy {
        int id;

        this(int i) { id = i; }

        override Teddy gift() { return this; }
}

Gift tearOpen(Gift)(Wrap!Gift wrappedGift)
{
    return wrappedGift.gift;
}

void main() {
    auto ted = new Teddy(123);
    auto r = tearOpen(ted); // NOOOO! Teddy!
    assert(r == ted); // Phew, Teddy was implicitly gift-wrapped
                      // and we tore the wrap open, not Teddy.
}

Reply via email to