Issue 6032 - wstring literals cannot be implicitly converted to const(wchar)*
Summary: wstring literals cannot be implicitly converted to const(wchar)*
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Windows
: P3 normal
Assignee: No Owner
URL:
Keywords: pull, rejects-valid
Depends on:
Blocks:
 
Reported: 2011-05-18 14:17 UTC by Andrej Mitrovic
Modified: 2023-07-14 19:12 UTC (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2011-05-18 14:17:58 UTC
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 kennytm 2011-05-18 21:07:53 UTC
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 Andrej Mitrovic 2011-05-18 21:38:15 UTC
I forgot about that, thanks for the reminder.
Comment 3 Elie Morisse 2015-04-21 19:02:57 UTC
   const(char)* s = "foo"c; // fails too 

Any rationale behind this?
Comment 4 yebblies 2015-04-22 02:38:36 UTC
(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 Steven Schveighoffer 2015-04-22 16:22:38 UTC
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 Dlang Bot 2023-07-14 11:49:16 UTC
@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 Nick Treleaven 2023-07-14 12:20:41 UTC
Changed the pull not to close this issue.
Comment 8 Dlang Bot 2023-07-14 19:12:47 UTC
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