void main() { const(char)* cptr = "foo"; // ok const(wchar)* wcptr = "foo"w; // error } Error: cannot implicitly convert expression ("foo"w) of type immutable(wchar)[] to const(wchar)* By fixing this it should eliminate the need to call std.utf.toUTF16z on string literals passed to WinAPI functions expecting zero-terminated wide strings. Note that dstrings suffer from the same issue: const(dchar)* dcptr = "foo"d; // error However I'm not sure if there are any C libraries which would take a UTF32 zero-terminated string. It would be consistent to make them all behave the same way though.
You could use the postfix-less version. A string literal is convertible to immutable(<any>char)[] and immutable(<any>char)*. (That should apply to "..."w though). const(wchar)* wcptr = "foo";
I forgot about that, thanks for the reminder.
const(char)* s = "foo"c; // fails too Any rationale behind this?
(In reply to Elie Morisse from comment #3) > const(char)* s = "foo"c; // fails too > > Any rationale behind this? Essentially that 'c' gives it an explicit type, so conversions are disabled.
So, should this be closed as WONTFIX? I kind of agree with OP that a specifically typed string is not really any different than an ambiguous one. In fact, the compiler still adds the trailing null character even if you specify 'w'. The one possibility I see that would be an issue is some type of overloading decision where you want to specifically choose the const(wchar)* version. Admittedly, this is very unlikely to occur in the wild.
@ntrel updated dlang/dlang.org pull request #3662 "[spec] Improve string literal docs" fixing this issue: - Fix Issue 6032 - wstring literals cannot be implicitly converted to const(wchar)* Until this works, it should not be documented. https://github.com/dlang/dlang.org/pull/3662
Changed the pull not to close this issue.
dlang/dlang.org pull request #3662 "[spec] Improve string literal docs" was merged into master: - 0ca0e63d81fc14b1610625693ee6aa9fd475f6c1 by Nick Treleaven: Document that StringPostfix disables implicit conversions Part of Issue 6032 - wstring literals cannot be implicitly converted to const(wchar)* https://github.com/dlang/dlang.org/pull/3662