Convert string to wchar.

Jonathan M Davis jmdavisProg at gmx.com
Tue Aug 2 10:51:39 PDT 2011


> I tried to convert a string into a wchar, but that didn't compile
> because of this template constraint:
> 
> https://github.com/D-Programming-Language/phobos/blob/master/std/conv.d#L17
> 70
> 
> Is there a way to convert a string into a wchar?

Does that even make sense? What do you want it to do, convert the first code 
point to a wchar and throw if there's more than one character in the string? 
That's like asking whether you can covert between a container of ints and an 
int. I would never expect std.conv.to to support that. Not to mention, you 
shouldn't normally be using char or wchar by themselves, because they might 
not be valid code points. Normally, only dchar should be used when 
representing an individual character. If you want this, I'd suggest that you 
simply do something like

cast(wchar)str.front

What you're asking for is inherently unsafe as far as unicode goes.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list