How to write a counterpart to C++ std::invoke that works with both free functions and methods?

60rntogo 60rntogo at gmail.com
Sat Sep 26 22:58:44 UTC 2020


I tried this:

---
auto invoke(alias fun, Args...)(Args args)
{
   return fun(args);
}
---

which works with free functions:

---
int add(int a, int b)
{
   return a + b;
}

assert(invoke!add(1, 2) == 3);
---

but with a method:

---
struct Foo
{
   bool isValid(int a)
   {
     return a > 0;
   }
}

auto foo = Foo();
assert(invoke!isValid(foo, 3));
---

I get the error "undefined identifier isValid". How can I make 
this work?


More information about the Digitalmars-d-learn mailing list