Member function forwarding

Andrey Zherikov andrey.zherikov at gmail.com
Thu Feb 17 20:59:43 UTC 2022


I have a struct that has struct data member and I'd like to 
forward some (not all) functions from internal data member. Some 
thing like this:
```d
struct A {
     void foo() const { writeln("A.foo"); }
}

struct B {
     A a;

     auto foo(ARGS...)(ARGS args) { return a.foo(args); }
     // alias foo = a.foo;
}

void main()
{
     B b;
     b.foo();   // In "alias" case: Error: `this` for `foo` needs 
to be type `A` not type `B`
}
```

Is there simpler way to achieve this?
I tried `alias foo = a.foo` but it doesn't work: "Error: `this` 
for `foo` needs to be type `A` not type `B`" - when function is 
called (`b.foo()`).

Another question: does `auto foo(ARGS...)(ARGS args) { return 
a.foo(args); }` correctly forward `ref`, `const` etc. arguments?


More information about the Digitalmars-d-learn mailing list