Implicit conversion to mutable if no indirections?

Paul Backus snarwin at gmail.com
Tue Sep 6 14:43:01 UTC 2022


On Tuesday, 6 September 2022 at 10:06:59 UTC, Quirin Schroll 
wrote:
> I ran into this while writing a DIP that “abused” `const`. In 
> C++, `const` is a far lower guarantee and it’s more like a 
> convention than actual compiler checking. I really dislike the 
> fact that Walter made `const(int*)` become `const(int)*` as 
> well as `const(int[])` become `const(int)[]` when an object of 
> the former types is passed to a function template as an 
> argument which has its type inferred:
> ```D
> void f(T)(T x) { pragma(msg, "parameter: ", T); }
> void main()
> {
>     const int[] xs;
>     pragma(msg, "argument : ", typeof(xs));
>     f(xs);
>     const int* p;
>     pragma(msg, "argument : ", typeof(p));
>     f(p);
> }
> ```

Fun fact: I actually had to work around this behavior a little 
while ago, in order to get `SumType` working correctly with 
`inout`. You can see the workaround and its application in this 
Phobos commit:

https://github.com/dlang/phobos/commit/a74fa63e6775d626850d8ebd854d9803c7ffb97d

I can't find the post, but think it was Andrei who once remarked 
on these forums that the adjustment of `qual(T[])` to `qual(T)[]` 
was all upside, with essentially zero cost. Well, here's your 
cost.

Special cases are like drugs. They can make you feel good in the 
short term, but sooner or later, the bill will always come due. 
Just Say No, kids.


More information about the Digitalmars-d mailing list