`ref T` should be a type!!
Manu
turkeyman at gmail.com
Sun Mar 31 22:48:57 UTC 2019
On Fri, Mar 29, 2019 at 9:10 AM Victor Porton via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
>
> That `ref T` (where T is a type) is not a type is a serious
> design error, because so `ref T` cannot be used as a template
> argument.
>
> It is a very serious problem.
>
> Even in C++ despite of all its silliness, T& is a type and can be
> used as a template argument.
>
> Can the language be changed to resolve this problem?
1. I couldn't possibly agree with you more!!! Sadly that's not how it is.
2. You can use `auto ref` on function arguments of template functions
to capture the ref-ness of the calling argument: void fun(T)(auto ref
T arg)
3. I have often deployed something like this (I just typed this in the
email as example, I have no idea if this compiles or runs):
Ref(T)
{
T* value;
this(ref T val) { value = &val; }
ref T opAssign(U)(auto ref U rh) { *value = rh; }
ref T get() { return *value; } inout;
alias get this;
}
...
template isRef(T) = is(T == Ref!U, U);
void fun(T)(T arg)
{
pragma(msg, isRef!T ? "is a ref" : "not a ref");
}
int i;
auto ri = Ref!int(i);
fun(i); // > "not a ref"
fun(ri); // > "is a ref"
Writing that code is like stabbing myself in the hands with rusty
forks... I feel emotionally unstabilised, but it can solve your
problems in various applications.
More information about the Digitalmars-d
mailing list