Implicit string lit conversion to wstring/dstring
Dmitry Olshansky
dmitry.olsh at gmail.com
Wed Mar 14 11:23:45 PDT 2012
On 14.03.2012 22:00, H. S. Teoh wrote:
> Code:
> import std.stdio;
> version(explicit) {
> void func(dstring s) {
> dstring t = s;
> writeln(t);
> }
> } else {
> void func(S)(S s) {
> dstring t = s; // line 10
> writeln(t);
> }
> }
> void main() {
> func("abc");
> }
>
> If version=explicit is set, the program compiles fine. But if not, then
> the compiler complains:
>
> test.d:10: Error: cannot implicitly convert expression (s) of type string to immutable(dchar)[]
>
> What do I need to do to make the template version of func trigger
> implicit conversion of the string lit to dstring? What I'm trying to do
> is to templatize dstring as well, but still benefit from the
> string->dstring conversion when instantiated with dstring.
>
> (Ditto with implicit conversion to wstring.)
>
>
> T
>
How about this (untested)?
void func()(dstring s) {
dstring t = s;
//... funcImpl(t);
}
void func(S)(S s) {
dstring t = to!dstring(s);
//... funcImpl(t);
}
--
Dmitry Olshansky
More information about the Digitalmars-d
mailing list