Why code failed to compile for foo2?

Steven Schveighoffer schveiguy at gmail.com
Tue Dec 14 15:14:40 UTC 2021


On 12/14/21 12:04 AM, Tejas wrote:
> Is there anything wrong with the answer I posted?
> 
> Can you please tell me if there's anything dissatisfactory about it? I 
> feel like it does everything the OP wants.
> 
> Also, am I wrong in using `Unconst` over `Unqual`? Isn't `Unqual` 
> overkill if you just want to cast away `const`?

Unqual is fine for value types, it should work in all cases.

The OP's problem is that it's entirely possible to build an overload set 
with *just* the unqualified types specified (in this case, an integral 
type), but there's not a way to express that and still have it work with 
IFTI.

In other words, if you have a call like:

```d
const int x;
foo2(x);
```

You want to have the parameter be mutable inside foo2. There currently 
isn't a way to express that if `foo2` is an IFTI template. The opposite 
is actually easily expressable:

```d
void foo2(T)(const(T) val)
{
    // only one instantiation of foo2 per const(int), immutable(int), int,
    // and now val is const, even if the argument is not
}
```

With a standard function it works just fine due to implicit conversion, 
but with IFTI, there's no way to express it because it goes through an 
alias. The closest you can come is to write a wrapper shim that calls 
the right instantiation. This should be OK as long as inlining is 
happening, but it seems like extra work for the optimizer, when it 
should be easy to express in the language somehow.

BTW, there is a related issue: https://issues.dlang.org/show_bug.cgi?id=1807

-Steve


More information about the Digitalmars-d-learn mailing list