如何铸造"void (MyClass::*)(int)" "void (*)(int)"?



我一直在寻找如何将类成员强制转换为C样式回调。

最近我发现了一个特殊绑定破解的答案,它允许将类成员绑定到C风格的回调:

https://stackoverflow.com/a/39524069/5405443

我有以下工作代码将函数MyClass::f绑定到C函数fB具有void(*((int(类型,Func模板参数具有void(MyClass::*((int(类型。

template<typename CB, typename Func, typename... Params>
CB* c_bind(std::_Bind<Func(Params...)> function) {
return Callback<typename ActualType<CB>::type, __COUNTER__, Func>::getCallback(function);
}
typedef void (cb_type)(int);
class MyClass {
public:
void f(int x) {
std::cout << "Hello from MyClass::f(int), value: " << x << std::endl;
}
};
int main() {
MyClass mc;
auto f = c_bind<cb_type>(std::bind(&MyClass::f, mc, std::placeholders::_1));
//                ^ how to avoid explicit callback type declaration here?
f(10);
return 0;
}

我还发现了这段代码(https://gist.github.com/vikchopde/73b62314379f733e8938f11b246df49c)用于"展开"某种函数。

bool ok = fu::is_unwrappable<decltype(&MyClass::f)>::value; // always false
// fu::unwrap_function<decltype(&MyClass::f)>::type::function_ptr blah; // won't compile

但由于我不知道的原因,它不会起作用。

我的问题是,是否有任何解决方法可以从具有类memeber指针的类型中提取返回类型和args列表,如void(MyClass::*((int(和构造类C类型的void(*((int

谢谢你的帮助!

在C++17中,允许将任意非类型参数传递给具有template<auto>的类。因此,我们可以将MyClass::f存储为模板参数,并使用decltype解析其类型。在将此类型传递给另一个模板化类之后,我们能够使用模板专门化提取所需的类型。

下面的代码显示了如何构造C样式函数wrapper<>::func_type

由于您似乎要将对象绑定到其成员函数,因此我还编写了演示代码,通过调用wrapper<>::bind来实现这一点。希望能有所帮助。

class MyClass {
public:
void f(int x) {
std::cout << "Hello from MyClass::f(int), value: " << x << std::endl;
}
};
void f(int x) {
std::cout << "Hello from f(int), value: " << x << std::endl;
}
template<auto F>
struct wrapper
{
template<typename> struct inner;
template<class Cls, typename Ret, typename... Args>
struct inner<Ret(Cls::*)(Args...)>
{
using func_type = Ret(Args...);
static auto bind(Cls *obj)
{
return [=](Args ...args){
return (obj->*F)(std::forward<Args>(args)...);
};
}
};
using func_type = typename inner<decltype(F)>::func_type;
static const constexpr auto bind = inner<decltype(F)>::bind;
};
int main() {
MyClass mc;
auto h = wrapper<&MyClass::f>::bind(&mc);
h(10);
using func_t = typename wrapper<&MyClass::f>::func_type;
std::function<func_t> g = f;
g(1);
return 0;
}

首先,我要感谢@Dappur提供了一个很好的例子。使用您的指南,我稍后将用std::_bind用法重写我丑陋的绑定接口。我还要感谢@Sam Varshavchik提到那套C++书。我将开始阅读它,成为像你一样的C++大师,学习如何">为什么我不能这样铸造它"。但不幸的是,以我糟糕的c++经验,我现在仍然可以做到。这是工作代码:

template<class T, unsigned int n, class CallerType>
struct CallbackWrapper;
template<class Ret, class... Params, unsigned int n, class CallerType>
struct CallbackWrapper<Ret(Params...), n, CallerType> {
static auto get(std::function<Ret(Params...)>&& fn) -> Ret(*)(Params...) {
func = fn;
return static_cast<Ret(*)(Params...)>(CallbackWrapper<Ret(Params...), n, CallerType>::callback);
}
private:
static std::function<Ret(Params...)> func;
static Ret callback(Params... args) {
return func(args...);
}
};
template<class Ret, class... Params, unsigned int n, class CallerType>
std::function<Ret(Params...)> CallbackWrapper<Ret(Params...), n, CallerType>::func;
template<typename T>
struct lambda_to_stdf {
using type = void;
};
template<typename Ret, typename Class, typename... Args>
struct lambda_to_stdf<Ret(Class::*)(Args...) const> {
using type = std::function<Ret(Args...)>;
};
template<class Ret, class Cls, class... Args1, class... Args2>
auto c_bind(std::_Bind<Ret(Cls::*(Cls, Args1...))(Args2...)> function) -> Ret(*)(Args2...) {
return CallbackWrapper<Ret(Args2...), __COUNTER__, Ret(Cls::*(Cls, Args1...))(Args2...)>::get(std::move(function));
}
template<class Ret, class... Args>
auto c_bind(std::function<Ret(Args...)> function) -> Ret(*)(Args...) {
return CallbackWrapper<Ret(Args...), __COUNTER__, std::function<Ret(Args...)>>::get(std::move(function));
}
template<class F>
auto c_bind(F function) -> decltype(c_bind((typename lambda_to_stdf<decltype(&F::operator())>::type)(function))) {
return c_bind((typename lambda_to_stdf<decltype(&F::operator())>::type)(function));
}

用法:

class MyClass {
public:
void f(int x) {
std::cout << "Hello from MyClass::f(int), value: " << x << std::endl;
}
};
int main() {
MyClass mc;
auto f = c_bind(std::bind(&MyClass::f, mc, std::placeholders::_1));
f(10);
std::function<void(int)> stdf = [](int v) {
std::cout << "hello from std::function, value: " << v << std::endl;
};
auto f2 = c_bind(stdf);
f2(100);
auto f3 = c_bind([](int v) -> int {
std::cout << "hello from lambda, value: " << v << std::endl;
return 5.0f;
});
f3(1000);
return 0;
}

希望这对某人有帮助。

相关内容

最新更新