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

mw mingwu at gmail.com
Wed Jun 17 22:09:35 UTC 2020


On Wednesday, 17 June 2020 at 21:49:29 UTC, Avrina wrote:
> void foo(T)(auto ref T v) if (is(T : int) == __traits(isRef, > 
> v)) {

Thanks for the suggestion.

Yes, that what I mean by: "guess I have to write more `static 
if`s."

That is ugly.


My thought is that: pointer and reference are all types of its 
own right:

typedef `int*` is ptr in C++
typedef `int&` is ref in C++

and can you see the beauty of this symmetry?  :-)


In D, alias can correctly treat int* as pointer type, but treat 
`ref int` as `int`, this break the symmetry

alias `int*`    is       ptr  in D
alias `ref int` becomes *int* in D

can you see the visual ugliness of this? :-) not even to mention 
the semantic ugliness:

compiler error on g(), not on f():
---------------------------
alias refInt = ref int;
alias ptrInt =     int*;

void f(refInt i) {
   i = 456;
}

void g(ptrInt i) {
}

void main() {
   int i = 123;
   writeln(i);  // 123
   f(i);
   writeln(i);  // 123! again

   g(i);   // Error: function `reft.g(int* i)` is not callable 
using argument types `(int)`
}
---------------------------



More information about the Digitalmars-d mailing list