Thread overview
argument type const char* can pass string, buf why const wchar* can not pass wstring
Dec 27, 2015
riki
Dec 27, 2015
Alex Parrill
Dec 27, 2015
riki
Dec 27, 2015
Basile B.
Dec 27, 2015
Basile B.
Dec 27, 2015
riki
Dec 27, 2015
Basile B.
Dec 27, 2015
Jimmy Cao
December 27, 2015
void ccf(const char* str){}
void cwf(const wchar* str){}

void main()
{
    ccf("aaa");    //ok
    cwf("xxx"w); // error and why ?
}
December 27, 2015
On Sunday, 27 December 2015 at 03:34:18 UTC, riki wrote:
> void ccf(const char* str){}
> void cwf(const wchar* str){}
>
> void main()
> {
>     ccf("aaa");    //ok
>     cwf("xxx"w); // error and why ?
> }

Unrelated to your error, but those functions should probably take a `string` and `wstring` respectively instead.
December 27, 2015
On Sunday, 27 December 2015 at 03:40:50 UTC, Alex Parrill wrote:
> On Sunday, 27 December 2015 at 03:34:18 UTC, riki wrote:
>> void ccf(const char* str){}
>> void cwf(const wchar* str){}
>>
>> void main()
>> {
>>     ccf("aaa");    //ok
>>     cwf("xxx"w); // error and why ?
>> }
>
> Unrelated to your error, but those functions should probably take a `string` and `wstring` respectively instead.

xx.d(7): Error: function xx.cwf (const(wchar*) str) is not callable using argument types (wstring)
December 27, 2015
On Sunday, 27 December 2015 at 03:34:18 UTC, riki wrote:
> void ccf(const char* str){}
> void cwf(const wchar* str){}
>
> void main()
> {
>     ccf("aaa");    //ok
>     cwf("xxx"w); // error and why ?
> }

IDK but usually the const storage class is used for narrow strings because it allows to pass either `char[]` or `string[]`:

```
void ccf(const char[] str){}
void cwf(const wchar[] str){}

void main()
{
    ccf("aaa");
    cwf("xxx"w);
    ccf("aaa".dup);
    cwf("xxx"w.dup);
}
```

I'm actually surprised that one works, maybe both should fail.
December 27, 2015
On Sunday, 27 December 2015 at 04:54:07 UTC, Basile B. wrote:
> it allows to pass either `char[]` or `string[]`:

I meant "char[]` or `string", string without square brackets of course...


December 27, 2015
On Sunday, 27 December 2015 at 04:54:07 UTC, Basile B. wrote:
> On Sunday, 27 December 2015 at 03:34:18 UTC, riki wrote:
>> void ccf(const char* str){}
>> void cwf(const wchar* str){}
>>
>> void main()
>> {
>>     ccf("aaa");    //ok
>>     cwf("xxx"w); // error and why ?
>> }
>
> IDK but usually the const storage class is used for narrow strings because it allows to pass either `char[]` or `string[]`:
>
> ```
> void ccf(const char[] str){}
> void cwf(const wchar[] str){}
>
> void main()
> {
>     ccf("aaa");
>     cwf("xxx"w);
>     ccf("aaa".dup);
>     cwf("xxx"w.dup);
> }
> ```
>
> I'm actually surprised that one works, maybe both should fail.

windows api is use const(wchar)*, not const wchar[]
December 27, 2015
On Sunday, 27 December 2015 at 03:34:18 UTC, riki wrote:
> void ccf(const char* str){}
> void cwf(const wchar* str){}
>
> void main()
> {
>     ccf("aaa");    //ok
>     cwf("xxx"w); // error and why ?
> }

You need to remove the w suffix.  Otherwise it is forcibly typed as a dynamic array and is no longer implicitly convertible.

void cwf(const wchar* str){}
void main()
{
    ccf("aaa");    //ok
    cwf("xxx"); // a string literal
}
December 27, 2015
On Sunday, 27 December 2015 at 05:29:44 UTC, riki wrote:
> On Sunday, 27 December 2015 at 04:54:07 UTC, Basile B. wrote:
>> On Sunday, 27 December 2015 at 03:34:18 UTC, riki wrote:
>>> void ccf(const char* str){}
>>> void cwf(const wchar* str){}
>>>
>>> void main()
>>> {
>>>     ccf("aaa");    //ok
>>>     cwf("xxx"w); // error and why ?
>>> }
>>
>> IDK but usually the const storage class is used for narrow strings because it allows to pass either `char[]` or `string[]`:
>>
>> ```
>> void ccf(const char[] str){}
>> void cwf(const wchar[] str){}
>>
>> void main()
>> {
>>     ccf("aaa");
>>     cwf("xxx"w);
>>     ccf("aaa".dup);
>>     cwf("xxx"w.dup);
>> }
>> ```
>>
>> I'm actually surprised that one works, maybe both should fail.
>
> windows api is use const(wchar)*, not const wchar[]

To be clear:

My remark was about how it's used in phobos.
In fact your usage is wrong since you should pass either:

"sfsdf".ptr"
"sdfsf"w.ptr

That's also why i said that I was surpsied that the first call didn't generate a compilation error. Anyway, it looks like there is an implicit convertion in this case...