Is this an auto ref bug?

Andrej Mitrovic none at none.none
Mon Apr 25 18:44:34 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));


More information about the Digitalmars-d-learn mailing list