Disallow arrays as pointers

Timon Gehr timon.gehr at gmx.ch
Tue Nov 1 03:25:37 PDT 2011


On 11/01/2011 01:38 AM, bearophile wrote:
> Timon Gehr:
>> extern(C) void foo(char* str);
>> foo(cast(char*)"hello");
>
> cast(char*) is D code, it's not a legacy C idiom, so you are writing it now in the D programs. Five years from now do you prefer D programmers to write:
>
> extern(C) void puts(const char* str);
> void main() {
>      puts(cast(char*)"hello");
> }
>

That works without the cast.

> Or:
>
> extern(C) void puts(const char* str);
> void main() {
>      puts("hello".ptr);
> }

That works without the ".ptr".

>
> ?
> Casts are unsafe and using the ptr is even shorter to write. I don't seen the need of that opCast.
>

I was casting to char*, not const(char*). It is just that occasionally C 
headers/bindings lack the const qualifier. Then passing a string literal 
is achieved simplest by a cast to char*. I don't think we really need 
that feature, I just think that removing it breaks existing code without 
a reason.

extern(C) void puts(char* str);
void main() {
      puts(cast(char*)"hello");
}


More information about the Digitalmars-d mailing list