inout question

Steven Schveighoffer schveiguy at yahoo.com
Tue Feb 13 16:39:29 UTC 2018


On 2/12/18 12:33 AM, Norm wrote:
> Hi,
> 
> I'm new to D so can someone explain to me what is happening here?
> 
> 
> void func(const char* s, char** e) {
>      import core.stdc.stdlib;
>      auto result = strtod(s, e);
> }
> 
> Error: function core.stdc.stdlib.strtod (scope inout(char)* nptr, scope 
> inout(char)** endptr) is not callable using argument types 
> (const(char*), char**)
> 
> I've found I have to use the following:
> 
> void func(inout (char)* s, inout(char)** e)
> 
> 
> I thought inout was supposed to take const or non-const variants, so 
> expected the original const char* s to work.
> 

A way to think about inout, when you don't understand why you can't call 
it, is to think what inout *should* resolve to, and then see if you can 
assign the existing data to the parameter type.

For example, in this case, you are calling:

strtod(inout(char)* nptr, inout(char)** endptr) with const char *, and 
char **.

Since const char * and char ** vary on mutability, the compiler is going 
to try const in place of inout.

So try it out:

// replace inout with const
const(char)* nptr = s; // ok
const(char)** endptr = e; // Error

I wish the error message was more specific, it would have made things 
obvious. But due to overloading, it's really hard to create errors that 
are good explanations, but not too verbose.

-Steve


More information about the Digitalmars-d-learn mailing list