digitalmars.D.learn - Member function forwarding
- Andrey Zherikov (25/25) Feb 17 2022 I have a struct that has struct data member and I'd like to
- Andrey Zherikov (6/8) Feb 17 2022 Actually the answer is `NO`. I have to do `auto foo(ARGS...)(auto
- Stanislav Blinov (9/17) Feb 18 2022 This also doesn't forward correctly. It'll always try to copy all
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?
Feb 17 2022
On Thursday, 17 February 2022 at 20:59:43 UTC, Andrey Zherikov wrote:Another question: does `auto foo(ARGS...)(ARGS args) { return a.foo(args); }` correctly forward `ref`, `const` etc. arguments?Actually the answer is `NO`. I have to do `auto foo(ARGS...)(auto ref ARGS args) { return a.foo(args); }`. Also if `a.foo` is a `const` function then I have to add `const` to this definition as well.
Feb 17 2022
On Thursday, 17 February 2022 at 21:17:02 UTC, Andrey Zherikov wrote:On Thursday, 17 February 2022 at 20:59:43 UTC, Andrey Zherikov wrote:This also doesn't forward correctly. It'll always try to copy all arguments. What would fowrard is ``` import core.lifetime : forward; auto foo(Args...)(auto ref Args args) { return a.foo(forward!args); } ```Another question: does `auto foo(ARGS...)(ARGS args) { return a.foo(args); }` correctly forward `ref`, `const` etc. arguments?Actually the answer is `NO`. I have to do `auto foo(ARGS...)(auto ref ARGS args) { return a.foo(args); }`.
Feb 18 2022