Why do "const inout" and "const inout shared" exist?

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Mon Jul 3 07:01:12 PDT 2017


On Sunday, July 02, 2017 01:16:13 ag0aep6g via Digitalmars-d wrote:
> On 07/01/2017 11:47 PM, Andrei Alexandrescu wrote:
> > Walter looked at http://erdani.com/conversions.svg and said actually
> > "const inout" and "const inout shared" should not exist as distinct
> > qualifier groups, leading to the simplified qualifier hierarcy in
> > http://erdani.com/conversions-simplified.svg.
>
> This may be a stupid question, but those graphs say: inout -> const, but
> inout may stand for shared and there's no shared -> const.
>
> How can inout -> const be allowed while shared -> const is forbidden?

It sounds _very_ broken to me if inout can mean shared. After all, the
compiler will treat various operations as illegal if a variable is marked
shared which would be legal if it weren't. So, if you have an inout
parameter accepting a shared argument, then you can break the protections
that shared is supposed to be giving. Also, the compiler is supposed to be
able to optimize based on the fact that a variable is thread-local and
guaranteed not to be shared across threads, and if inout can mean shared,
then a function using inout no longer has the guarantee that the data is
thread-local and can't optimize based on that inforamtion.

Fortunately, it looks like your assertion that shared may stand for inout is
wrong, because this code fails to compile:

class C
{
}

inout(C) foo(inout(C) c)
{
    return c;
}

void main()
{
    shared a = new C;
    auto b = foo(a);
}

and gives the error

test.d(13): Error: function test.foo (inout(C) c) is not callable using 
argument types (shared(C))

- Jonathan M Davis


More information about the Digitalmars-d mailing list