Generate a pointer to a method of a struct
kdevel
kdevel at vogtner.de
Sat Oct 15 01:48:15 UTC 2022
On Saturday, 15 October 2022 at 00:31:47 UTC, Iain Buclaw wrote:
>> ```
>> auto funcptr (alias method) ()
>> {
>> return &method;
>> }
>> :
>> fun = funcptr!bar;
>> :
>> ```
>>
>> Which works but neither dmd nor gdc were able to optimize the
>> additional function call away.
>
> pragma(inline, true)
> auto funcptr (alias method) ()
Thanks. Just found that
```
template funcptr (alias method) {
enum funcptr = &method;
}
```
works on both dmd and gdc. This gives rise to questions:
Is this code expected to compile and pass its unittest?
```
struct S {
void bar () { }
}
enum ei = &S.bar;
immutable i = &S.bar; // line 6
const c = &S.bar; // line 7
unittest {
import std.stdio;
writeln (ei);
writeln (i);
writeln (c);
}
```
gdc passes while dmd says:
```
$ dmd -unittest -main -run ini
ini.d(6): Error: non-constant expression `& bar`
ini.d(7): Error: non-constant expression `& bar`
```
Is this consistent?
More information about the Digitalmars-d-learn
mailing list