Is this an auto ref bug?

Jonathan M Davis jmdavisProg at gmx.com
Mon Apr 25 19:22:16 PDT 2011


> import std.traits;
> 
> void main()
> {
>     const wchar* t;
>     unqual(t);
> }
> 
> auto ref unqual(T)(ref T value)
> {
>     return cast(Unqual!T)value;
> }
> 
> I've attempted to create myself an unqual function which could for example
> do a cast from const char* to char*. The above won't work though:
> test.d(14): Error: variable test.unqual!(const(char*)).unqual.value cannot
> modify const
> 
> It does work if I only use auto:
> auto unqual(T)(ref T value)
> {
>     return cast(Unqual!T)value;
> }
> 
> But that creates an rvalue.
> 
> I wanted to use such a function to avoid doing explicit casts to get an
> unqualified type. For example a C function might be prototyped as:
> 
> foo(char* input);
> 
> You can't pass a const char* here, so you would have to either cast the
> type: const char* str;
> foo(cast(char*)str);
> 
> Or you would modify the prototype to:
> foo(const(char)* input);
> foo(str);  // now ok
> 
> The second choice is probably the best, since this specific C function
> would not actually modify the input. But I thought having an "unqual"
> function would be handy, so I could use:
> 
> foo(unqual(str));

Okay. First off, T does _not_ include the ref. So, Unqual!T is not going to be 
a ref (and it doesn't look like you can cast to ref either, which does make a 
fair bit of sense). So, the result of the cast is a temporary, and you can't 
have a ref to a temporary, so it's illegal to return auto ref from the 
function. auto works because then the return type is just wchar* - there's no 
ref there. I really don't think that it works to change the type of a ref.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list