5

How can I print inout parameters?

import std.stdio;

void main()
{
    foo(2);
    return;
}

inout(int) foo(inout(int) x)
{
    writeln(x);
    return x;
}

Compiler output:

c:\D\dmd2\windows\bin\..\..\src\phobos\std\conv.d(3881): Error: template instance Unqual!(__T4ImplTNgiZ) does not match template declaration Unqual(T)
c:\D\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(757): Error: template instance std.conv.toTextRange!(inout(int), LockingTextWriter) error instantiating
c:\D\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(1708):        instantiated from here: write!(inout(int), char)
x.d(11):        instantiated from here: writeln!(inout(int))
c:\D\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(1708): Error: template instance std.stdio.File.write!(inout(int), char) error instantiating
x.d(11):        instantiated from here: writeln!(inout(int))
x.d(11): Error: template instance std.stdio.writeln!(inout(int)) error instantiating

Also, to!string doesn't work with the inout parameter.

Update: I've just found out (by trial and error) that the following works:

writeln(cast(const)x);

Is this the normal method or am I missing something?

2
  • Not helpful in finding a solution to your problem, but what do you want to achieve with the inout attribute in front of the return type? If this was a method, it would would mean that this would be declared as inout. In that case I'd rather do int foo(inout(int) x) inout, much like you would use const or pure. Since this is a free function, I assume you meant someting like inout(int) foo(inout(int) x). But since int is a primitive type and will be copied anyway, you could as well leave it out. With compound/constructed types, this is a whole other story. Commented Jul 4, 2013 at 8:07
  • You are absolutely right, thanks, copied wrong code... What I wanted to achieve: just testing how inout return type changes according to inout parameters.
    – gerleim
    Commented Jul 4, 2013 at 11:13

1 Answer 1

3

This is a library bug, most likely in Unqual. I added it here: http://d.puremagic.com/issues/show_bug.cgi?id=10544

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.