Skip to content

[Missing feature] Possible solution for 'const ref' translation #19

@Dgame

Description

@Dgame

Hi, great project you have here. I'v read that one of your missing features is the const ref problem:
Hard because, unlike in C++, in D we can't pass a rvalue to a const ref parameter
That is sadly true and the current built-in solution (auto ref) only works in combination with templates and leads to template bloat, but there is a cheap-trick to solve this issue, maybe it'll help you out:

struct A
{
    private A* self = null;
    public string name = null;

    this(string name)
    {
        this.name = name;
    }

    @nogc
    ref A asRef()
    {
        this.self = &this;

        return *this.self;
    }
}

void test(ref A a1, ref const A a2)
{
    assert(a1.name == "foo");
    assert(a2.name == "bar");
}

void main()
{
    test(A("foo").asRef, A("bar").asRef);
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions