How to port C++ std::is_reference<T> to D ?
Q. Schroll
qs.il.paperinik at gmail.com
Mon May 11 19:08:09 UTC 2020
On Saturday, 9 May 2020 at 13:44:27 UTC, Per Nordlöw wrote:
> On Wednesday, 6 May 2020 at 17:46:28 UTC, Q. Schroll wrote:
>
>> C++'s decision to make references part of the type has some
>> advantages, but D didn't do it because of many disadvantages.
>
> Can you outline or give a link describing the advantages of
> C++'s vs D's choice in this matter?
Whether something is an advantage is subjective at least in some
sense. So whether you in particular consider something I'll list
here an advantage is basically your choice.
1. You can have variables ("data members") of reference type in
structs. (They work like head-const pointers; if D had head-const
or at least head-const pointers, those would be practically the
same, only that references cannot be null.)
2. Templates can manipulate ref-ness and so on by type building.
Notably, I had trouble writing templates that handle non-copyable
types correctly at ctfe. The reason was that implicit moving
(moving of temporaries) is done entirely by the compiler, but
manual moving (std.algorithm.mutation.move) does do stuff (it
doesn't just trick the compiler into moving casting stuff to
rvalue references).
3. You can have local variables that are references. While not
really different from pointers, I think they look less scary than
pointers.
4. Especially in casts, you can trick the compiler into doing
things without the fear of generating unnecessary code (in C++,
std::move and std::forward are mere casts that you could do
yourself but look awkward).
Potentially, I'm missing something.
Personally, I think D did the right thing making references a
storage class. You can see very early in learning C++ that
references are only half-way type constructors: They are not
fully composable. Normally, you can have arrays and pointers to
any type, but in C++ you can't have pointers to / arrays of
references.
More information about the Digitalmars-d-learn
mailing list