How to check that a member function is generated by compiler?
Andrey Zherikov
andrey.zherikov at gmail.com
Fri Feb 25 14:25:22 UTC 2022
On Friday, 25 February 2022 at 05:25:14 UTC, Ali Çehreli wrote:
> On 2/24/22 20:44, Andrey Zherikov wrote:
>
>> How can I check that `opAssign` is generated by compiler and
>> doesn't exist in the original code?
>
> I think this one:
>
> https://dlang.org/phobos/std_traits.html#hasElaborateAssign
>
> Ali
This take a struct as an argument, not a member, so I need to do
`if(sym == "opAssign")`. I gave this another thought and checking
the symbol against `"opAssign"` would be good enough for my use
case.
Another interesting observation - is there any explanation why
`typeof` returns different results for generated `opAssign`?
```d
import std.sumtype: SumType;
struct A
{
SumType!int b;
}
static foreach(sym; __traits(allMembers, A))
{
// ref A(A p) return,opAssign
pragma(msg,
typeof(__traits(getMember, A,
sym)).stringof,",",sym);
// true,pure nothrow @nogc ref @safe A(A p) return,opAssign
pragma(msg,
is(typeof(__traits(getMember, A, sym)) ==
function),",",
typeof(__traits(getMember, A,
sym)).stringof,",",sym);
// pure nothrow @nogc ref @safe A(A p) return,opAssign
pragma(msg,
typeof(__traits(getMember, A,
sym)).stringof,",",sym);
}
```
If I move `foreach` loop into a function (e.g. `main`) then the
first pragma prints the same as the others.
More information about the Digitalmars-d-learn
mailing list