Implicit conversion to mutable if no indirections?

Steven Schveighoffer schveiguy at gmail.com
Sat Sep 3 01:50:05 UTC 2022


On 9/2/22 2:58 PM, Ali Çehreli wrote:
> An issue I have is non-mutable expressions without indirections not 
> converting to mutable. I talked about this before and mentioned Unqual 
> at DConf 2022 to explain how it solves this.
> 
> The problem happens in template code:
> 
> void main() {
>    const i = 42;  // const, so foo's T will be const below
>    foo(i);
> }
> 
> void foo(T)(T value) {
>    T result;
>    ++result;  // Compilation error; but should it work?
> }
> 
> I feel like it should work because 'result' is a local int.

result is a local `const int`. That's because `T` is a `const int`.

IFTI uses the exact type passed, not a different type.

> But I am aware that we can't deduce T to be 'int' because we would be 
> losing that qualifier and further template deductions would be wrong. :/

It's not just that, there is no syntax to say to IFTI, "if you match 
type T, I really want you to use type U". It would actually be nice, but 
I don't know how it can be done.

The one exception to the "exact type" rule is for arrays and pointers, 
which automatically convert to their tail versions. This is a special 
case for the benefit of range code.

Perhaps the same rules should be done for value types?

-Steve


More information about the Digitalmars-d mailing list