Conversion string->int

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 7 16:26:14 PDT 2014


On Sat, 07 Jun 2014 21:22:03 +0000
Paul via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com> wrote:

> On Saturday, 7 June 2014 at 21:15:37 UTC, monarch_dodra wrote:
> > On Saturday, 7 June 2014 at 20:53:03 UTC, Paul wrote:
> >> I can not understand, why this code works:
> >>
> >>    char s[2] = ['0', 'A'];
> >>    string ss = to!string(s);
> >>    writeln(parse!uint(ss, 16));
> >>
> >> but this can deduces template:
> >>
> >>    char s[2] = ['0', 'A'];
> >>    writeln(parse!uint(to!string(s), 16));
> >>
> >> What's the reason? And what is the right way to parse
> >> char[2]->int with radix?
> >
> > parse takes an lvalue to a range. char[2] is a static array,
> > and is not a range. You need to store an actual "char[]" (or
> > string) in a variable to call parse. So "ss" will work,
> > "to!string(s)" will not.
>
> But to!string(s) creates temporary string object or I am not
> right? And this temp string is pushed as argument of parse... Or
> not?

to!string will allocate a string if it's passed anything other than string.
However, parse requires an lvalue, and to!string returns an rvlaue, so passing
the result of to!string directly to parse isn't going to work. You have to
assign it to a variable first.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list