Make `& Class.foo` illegal

Nathan S. no.public.email at example.com
Thu Aug 27 13:01:45 UTC 2020


On Thursday, 27 August 2020 at 11:20:17 UTC, Johan wrote:
> Currently this code is not rejected by the compiler and instead 
> creates non-functioning (undefined) code:
> ```
> struct S {
>     void foo() {}
> }
>
> void main() {
>     auto a = &S.foo;
> }
> ```
>
> See https://issues.dlang.org/show_bug.cgi?id=21195 .
>
> Considering https://www.digitalmars.com/articles/b68.html, why 
> not simply reject `&S.foo` during semantic analysis?
>
> -Johan

I sometimes compare member functions addresses to see if a class 
overrides a certain function. For example:

---
template hasDefaultOpEquals(C) if (is(C == class))
{
     enum hasDefaultOpEquals = &C.opEquals is &Object.opEquals;
}

class C1 {}

class C2 {
     int value;
     override bool opEquals(const Object rhs) const @safe {
         if (auto o = cast(typeof(this)) rhs) return value == 
o.value;
         return false;
     }
}

static assert(hasDefaultOpEquals!C1);
static assert(!hasDefaultOpEquals!C2);
---



More information about the Digitalmars-d mailing list