refInt = ref int: how to achieve this? or is this a bug?

Stanislav Blinov stanislav.blinov at gmail.com
Wed Jun 17 23:48:52 UTC 2020


On Wednesday, 17 June 2020 at 23:24:32 UTC, mw wrote:
> On Wednesday, 17 June 2020 at 23:09:55 UTC, Stanislav Blinov 
> wrote:
>>> it's the compiler's bug not rejecting it, given it's current 
>>> semantics; instead it *silently* treat alias `ref int` as 
>>> `int`.
>>
>> It is both. In your code, for trying to use `ref` as a type 
>> qualifier when it is not (see [1] for type qualifiers), and in
>
> Given the current language semantics, right, it shouldn't be 
> typedef/alias-ed,
>
> But it make the code so complex / ugly to write:
>
> either:
>
> void foo(T)(auto ref T v) if (is(T : int) == __traits(isRef, 
> v)) {}

Which translates to "take everything that implicitly converts to 
int by reference, and everything else - by value". I would love 
to see that same intent expressed in C++ in one declaration.

> or
>
> f(    T) /*more static if here*/ { /*potential dup code here*/ }
> f(ref T) /*more static if here*/ { /*potential dup code here*/ }
>
> I would call for improvement.

Again, looking at your code, this all comes out of your trying to 
conflate references and pass by reference. References (i.e. 
classes) and values (i.e. primitive types and structs) are 
semantically different, so should be treated differently.




More information about the Digitalmars-d mailing list