Implicit string lit conversion to wstring/dstring

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Mar 14 11:16:11 PDT 2012


On Wed, Mar 14, 2012 at 07:00:35PM +0100, Alex Rønne Petersen wrote:
> On 14-03-2012 19: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
> >
> 
> I doubt that such an implicit conversion even exists. You have to
> prefix the string appropriately, e.g.:

OK, maybe implicit conversion is the wrong word. The compiler is
obviously interpreting func("abc") as func("abc"d) when we declare
func(dstring). But when we declare func(S)(S), the compiler deduces
"abc" as string and sets S=string.

What I want is to force the compiler to deduce S=dstring when I declare
func(S)(S) and call it as func("abc").


T

-- 
Дерево держится корнями, а человек - друзьями.


More information about the Digitalmars-d mailing list