简体   繁体   English

绑定成员函数指针

[英]Bound member-function pointer

I was wondering why there is no concept like "bound member-function pointer", ie a member function pointer that is bound to a specific object and can therefore can be treated like a normal function pointer, eg我想知道为什么没有像“绑定成员函数指针”这样的概念,即绑定到特定对象的成员函数指针,因此可以像普通函数指针一样对待,例如

#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>;

Yes.是的。 Use lambda's使用 lambda 的

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 .编辑 2:为此使用std::bind有巨大的开销......看看这个 Lambda's are practically zero cost. Lambda 的成本几乎为零。

Yes ,是的

you could use Lambda expressions, that from c++11 have replaced all these binding problem.你可以使用Lambda表达式,从 c++11 开始已经取代了所有这些绑定问题。

See this link for more details: Lambda Expression有关更多详细信息,请参阅此链接: Lambda 表达式

Remember that from C++14 the lambda expressions are also polymorphic.请记住,从 C++14 开始,lambda 表达式也是多态的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM