Thread overview
Why are string literals dual typed?
Mar 25, 2004
Manfred Nowak
Mar 25, 2004
Walter
Mar 27, 2004
Manfred Nowak
March 25, 2004
void f(char[] s){ }
void f(char* s){ }
void main(){
  f("");
}

produces:
function f overloads void(char[]s) and void(char*s) both match argument
list for f

This vanishes if the string literal is cast to one of both.

So why is it this way? And can this dual typedness be achieved for user generated types as well?

So long!


March 25, 2004
"Manfred Nowak" <svv1999@hotmail.com> wrote in message news:c3vluo$2esh$1@digitaldaemon.com...
> void f(char[] s){ }
> void f(char* s){ }
> void main(){
>   f("");
> }
>
> produces:
> function f overloads void(char[]s) and void(char*s) both match argument
> list for f
>
> This vanishes if the string literal is cast to one of both.
>
> So why is it this way? And can this dual typedness be achieved for user generated types as well?

It's that way because I couldn't justify tying it to one purpose. No. <g>


March 27, 2004
Walter wrote:

> It's that way because I couldn't justify tying it to one purpose. No. <g>

I do not find this very funny, I find it inconsistent.

On one hand if a string literal is used as a parameter of a C function, especially printf, it _must_ be considered as a D-string to avoid an AV, on the other hand if it is used as a parameter of a D function it must be considered _not_ to be a D-string.

To install consistent behaviour I see some possibilities:

1) leaving the dual typedness as it is and the need to disambiguate the type of a string literal for D-functions, and therefore disallow the usage of uncasted string literals as parameters of C functions.

2) giving up the dual typedness, and treat string literals as D-strings

3) leaving the dual typedness, but only as a conceptual phrase, by implicit casting to C-strings if used as a parameter to C functions and implicit casting to D-strings in all other casses of usage.

So long!