Bug 6032 – wstring literals cannot be implicitly converted to const(wchar)*

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2011-05-18T14:17:58Z
Last change time
2024-12-13T17:55:17Z
Keywords
pull, rejects-valid
Assigned to
No Owner
Creator
Andrej Mitrovic
Moved to GitHub: dmd#18342 →

Comments

Comment #0 by andrej.mitrovich — 2011-05-18T14:17:58Z
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.
Comment #1 by kennytm — 2011-05-18T21:07:53Z
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";
Comment #2 by andrej.mitrovich — 2011-05-18T21:38:15Z
I forgot about that, thanks for the reminder.
Comment #3 by syniurge — 2015-04-21T19:02:57Z
const(char)* s = "foo"c; // fails too Any rationale behind this?
Comment #4 by yebblies — 2015-04-22T02:38:36Z
(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.
Comment #5 by schveiguy — 2015-04-22T16:22:38Z
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.
Comment #6 by dlang-bot — 2023-07-14T11:49:16Z
@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
Comment #7 by nick — 2023-07-14T12:20:41Z
Changed the pull not to close this issue.
Comment #8 by dlang-bot — 2023-07-14T19:12:47Z
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
Comment #9 by robert.schadek — 2024-12-13T17:55:17Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18342 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB