www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Error: template std.conv.parse cannot deduce function from argument

reply Ralph Doncaster <nerdralph github.com> writes:
I get this error when I try the following code:

struct Record {
     union { ubyte[8] bytes; ulong l;}
     uint key;
}

Record r;
r.l = parse!ulong("deadbeef", 16);

However the following works:
string s = "deadbeef";
r.l = parse!ulong(s, 16);

And another way that works:
r.l = "deadbeef".to!ulong(16);

I've been doing C/C++ for over 20 years and recently started 
playing with D.  I think the template overloading for parse is 
getting confused because it sees the lvalue r.l as being of type 
union rather than ulong.  Is this a bug or the way things are 
supposed to work?
Feb 06
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Tuesday, 6 February 2018 at 17:33:43 UTC, Ralph Doncaster 
wrote:
 I get this error when I try the following code:
parse specifically works with a reference input it can advance. From the docs: "It takes the input by reference. (This means that rvalues - such as string literals - are not accepted: use to instead.) It advances the input to the position following the conversion."
 I think the template overloading for parse is getting confused 
 because it sees the lvalue r.l as being of type union rather 
 than ulong.
The left hand side of an assignment never plays a role in overloading, so you can disregard that. It is just a string literal cannot be advanced and thus does not work with parse.
Feb 06
parent Ralph Doncaster <nerdralph github.com> writes:
On Tuesday, 6 February 2018 at 17:47:30 UTC, Adam D. Ruppe wrote:
 On Tuesday, 6 February 2018 at 17:33:43 UTC, Ralph Doncaster 
 wrote:
 I get this error when I try the following code:
parse specifically works with a reference input it can advance. From the docs: "It takes the input by reference. (This means that rvalues - such as string literals - are not accepted: use to instead.) It advances the input to the position following the conversion."
 I think the template overloading for parse is getting confused 
 because it sees the lvalue r.l as being of type union rather 
 than ulong.
The left hand side of an assignment never plays a role in overloading, so you can disregard that. It is just a string literal cannot be advanced and thus does not work with parse.
Thanks! Got it.
Feb 06