0

I was wondering why there is no concept like "bound member-function pointer", i.e. a member function pointer that is bound to a specific object and can therefore can be treated like a normal function pointer, e.g.

#include <functional>

struct Foo {
    void foo() {} 
};

void foo() {}

void call(void(*)()) {}

template<typename T>
void call_mem(void(T::*fn)(), T* x) { (x->*fn)(); }

int main() {
    Foo x{};
    call(&foo); // works fine
    call_mem<Foo>(&Foo::foo, &x); // okay, why though
    call(std::bind(std::mem_fn(&Foo::foo), &foo)); // doesnt work and looks horrible + return type of mem_fn is unspecified
    call(std::bind(std::function(&Foo::foo), &foo)); // doesnt work
    // something like this?
    call(&x::foo); // imaginary
    call(&Foo::foo(x)); // imaginary
}

the best I could come up with is this

#include <memory>

void call(Function<void> *fn) { (*fn)(); }

struct Foo {
  void foo() {}
};

void foo() {}

int main(int argc, char **argv) {
  auto x = [] {};

  Foo f{};
  std::unique_ptr<Function<void>> fn0{new Function{&foo}};
  std::unique_ptr<Function<void>> fn1{new MemberFunction{&f, &Foo::foo}};
  std::unique_ptr<Function<void>> fn2{new MemberFunction{x}};

  call(fn0.get());
  call(fn1.get());
  call(fn2.get());
}

using this utility class

#include <type_traits>
#include <utility>

template <typename R, typename... Args>
struct Function {
  using Fn = R (*)(Args...);

  Fn fn;

  explicit Function(Fn fn) : fn{fn} {}

  virtual R operator()(Args... args) noexcept(
      noexcept(fn(std::forward<Args>(args)...))) {
    return fn(std::forward<Args>(args)...);
  }
};

template <typename R, typename... Args>
Function(R (*)(Args...)) -> Function<R, Args...>;

template <typename T, typename = std::void_t<>>
struct MemberFunction;

template <typename T, typename R, typename... Args>
struct MemberFunction<R (T::*)(Args...)> final : Function<R, Args...> {
  using Fn = R (T::*)(Args...);

  T *obj;
  Fn fn;

  MemberFunction(T *obj, Fn fn)
      : Function<R, Args...>{nullptr}, obj{obj}, fn{fn} {}

  R operator()(Args... args) noexcept(
      noexcept((obj->*fn)(std::forward<Args>(args)...))) override {
    return (obj->*fn)(std::forward<Args>(args)...);
  }
};

template <typename T, typename R, typename... Args>
struct MemberFunction<R (T::*)(Args...) const> : Function<R, Args...> {
  using Fn = R (T::*)(Args...) const;

  const T *obj;
  Fn fn;

  MemberFunction(const T *obj, Fn fn)
      : Function<R, Args...>{nullptr}, obj{obj}, fn{fn} {}

  R operator()(Args... args) noexcept(
      noexcept((obj->*fn)(std::forward<Args>(args)...))) final override {
    return (obj->*fn)(std::forward<Args>(args)...);
  }
};

template <typename T>
struct MemberFunction<T, std::void_t<decltype(&T::operator())>> final
    : MemberFunction<decltype(&T::operator())> {
  explicit MemberFunction(const T &obj)
      : MemberFunction<decltype(&T::operator())>{&obj, &T::operator()} {}
};

template <typename T>
MemberFunction(T) -> MemberFunction<T>;

template <typename T, typename R, typename... Args>
MemberFunction(T *, R (T::*)(Args...)) -> MemberFunction<R (T::*)(Args...)>;

template <typename T, typename R, typename... Args>
MemberFunction(const T *, R (T::*)(Args...) const)
    -> MemberFunction<R (T::*)(Args...) const>;
Yamahari
  • 1,570
  • 5
  • 17
  • 1
    In modern C++, lambda expressions have replaced all this binding... lambda expression have way less overhead... and since C++14 they are polymorphic. – JHBonarius Sep 23 '20 at 13:18
  • 1
    Use lambdas or std::bind to bind data to callables. Use std::function to have containers full of such callables. Do not use function pointers unless you need to interface with non-C++ module. – Öö Tiib Sep 23 '20 at 13:27
  • okay but `[&x]{x.foo();}` seems verbose, some syntax sugar like `&x::foo` seems appropriate and makes the intent much clearer ( in my opinion ) – Yamahari Sep 23 '20 at 14:01
  • @Yamahari with a lamba you can store it in a variable regardless of the type called. How would you save a vector of function caller, with first one calling `&x::foo` and second one `&y::bar` with `x` and `y` not related? – Moia Sep 23 '20 at 14:16
  • what I meant was, `&x::foo` could be syntax sugar for `[&x]{x.foo();}` – Yamahari Sep 23 '20 at 14:21

2 Answers2

1

Yes. Use lambda's

class Foo {
public:
    void Bar() const {}
};

#include <functional>

void Baz(std::function<void()> const& fun) {
    fun();
}


int main() {
    Foo foo;

    std::function<void()> fooBar = [&foo]() { foo.Bar(); };

    Baz(fooBar);
}

edit: more reading here

edit 2: using std::bind for this has massive overhead... Just look at this. Lambda's are practically zero cost.

JHBonarius
  • 6,889
  • 3
  • 12
  • 28
  • yea but how do you pass this lambda to another function without making it templated, capturing lambda cant decay to a function pointer – Yamahari Sep 23 '20 at 13:27
  • `std::function` is the tool you want here. – Marshall Clow Sep 23 '20 at 13:29
  • @MarshallClow yeah, updated. His question was not clear on his intention. Now it is. – JHBonarius Sep 23 '20 at 13:34
  • okay, then my question decays (ha) to: why do we need to explicitly declare a lambda like this, some syntax sugar like the one suggested `&foo::foo` seems appropriate and more readable ( in my opinion ) – Yamahari Sep 23 '20 at 13:55
  • @Yamahari because of the overhead... look at the link in my edit. – JHBonarius Sep 23 '20 at 14:02
  • no i mean `&foo::foo` could just result in a lambda like `[&foo]{foo.foo();}` just syntax sugar – Yamahari Sep 23 '20 at 14:09
  • @Yamahari because that's just not how the C++ syntax works. You can write [a proposal to the C++ committee](https://isocpp.org/std/submit-a-proposal), but I don't think it would have much chance... – JHBonarius Sep 23 '20 at 14:13
  • @Yamahari lambda are meant to hide the context. They are called anonymus classes. If you're working in only one context X you don't need those, but if you want to use in multiple contexts at the same time there is where lambda come into play – Moia Sep 23 '20 at 14:20
0

Yes,

you could use Lambda expressions, that from c++11 have replaced all these binding problem.

See this link for more details: Lambda Expression

Remember that from C++14 the lambda expressions are also polymorphic.

Zig Razor
  • 2,907
  • 2
  • 8
  • 27