In the following code, the `foo` function doesn't work when casting to an immutable or shared type. Can anyone please explain what is happening here? Is there any way of returning such variables byRef from a malloc'd chunk of memory?

import core.stdc.stdlib;

ref T foo(T)()
{
        int* foo = cast(int*) malloc(int.sizeof * 8);

        return *foo;
}

void main(string[] args)
{
        // Works!
        auto bar = foo!(int)();

        // Works!
        auto bar = foo!(const(int))();

        // Error: cast(immutable(int))*foo is not an lvalue
        auto bar = foo!(immutable(int))();

        // Error: cast(shared(int))*foo is not an lvalue
        auto bar = foo!(shared(int))();
}

Reply via email to