Why do I get this error when casting to an immutable or shared byRef return type?

Gary Willoughby via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 19 03:45:25 PDT 2016


On Sunday, 19 June 2016 at 10:35:59 UTC, Gary Willoughby wrote:
> ...

A more correct example:

import core.stdc.stdlib;
import std.traits;

ref T foo(T)()
{
	alias Type = Unqual!(T);

	Type* foo = cast(Type*) malloc(Type.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))();
}


More information about the Digitalmars-d-learn mailing list