绑定的成员函数指针
Bound member-function pointer
我想知道为什么没有像";绑定成员函数指针";,即绑定到特定对象的成员函数指针,因此可以像正常函数指针一样处理,例如
#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
}
我能想到的最好的就是这个
#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());
}
使用该实用程序类
#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>;
是。使用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);
}
编辑:更多阅读这里
编辑2:为此使用std::bind
有巨大的开销。。。看看这个。Lambda实际上是零成本的。
是,
您可以使用Lambda表达式,它已经取代了c++11中的所有这些绑定问题。
有关更多详细信息,请参阅此链接:Lambda Expression
请记住,在C++14中,lambda表达式也是多态的。
相关文章:
- 如何正确编写指针函数声明?
- C++常规指针函数或模板
- 如何重新定义 C++ 指针函数?
- C++ 指向其他类函数的指针函数
- 指针到指针函数参数
- 将指向成员的指针函数传递到模板中
- C++ 在 none 常量指针函数中返回一个常量指针
- 如何使用指针函数编写/读取数组
- 是C 中的函数指针函数对象
- 如何从另一个类调用指向成员的指针函数
- 如何声明采用指向成员的指针函数的函数
- 如何构造一个以可变参数指针函数作为成员的类?
- c 通过值或指针函数语法
- 将typedef方法作为指针函数传递
- 从 Main 中的双指针函数打印出指针数组
- strcpy 对指针函数的引用
- 这是否仍然声明一种指针函数的别名?
- 将指向成员的指针函数与 std::shared_ptr 结合使用
- 'Incomplete type' 为标准::函数声明指向成员的指针函数模板参数时出错
- 从注入进程的 DLL 调用函数并更改指针函数的地址