gpt4 book ai didi

c++ - 如何返回使用参数包定义的成员函数的 std::function 对象?

转载 作者:行者123 更新时间:2023-11-30 02:13:45 24 4
gpt4 key购买 nike

我想定义一个成员函数,它可以返回另一个成员函数的 std::function 对象。一般来说,这很容易。但是如果那个成员函数是通过参数包来定义的,我不知道该怎么做。

我写了一些代码如下,

class Example {
public:

template<typename... Args>
double getSome(Args... args) {
// do something
}

template<typename... Args>
std::function<double(Args...)> getSomeFun() {
return std::bind(&Example::getSome<Args...>, this);
}

};

这看起来是正确的,不幸的是。当我在 main() 方法中调用 getSetArgsFunction() 时,我得到了很多错误信息。在 main() 方法中,我的代码如下,

int main() {
Example e;
e.getSomeFun()(1.0, 2.0, 3.0);
}

我尝试编译它,但是 gcc 打印出如下错误,

error: no match for call to ‘(std::function<double()>) (double, double, double)’

那么,我们该怎么办呢?


更新

感谢@Quentin,但他/她的提议也无法解决问题。完整且最小的示例代码如下:

#include <functional>
#include <iostream>

class Example {
public:

template<typename... Args>
double getSome(Args... args) {
for (auto x : {args...}) {
std::cout << x << '\n';
}
}

template<typename... Args>
std::function<double(Args...)> getSomeFun() {
return std::bind(&Example::getSome<Args...>, this);
}

};

int main() {
Example e;
e.getSomeFun<double, double, double>()(1.0, 2.0, 3.0);
}

错误信息是:

In file included from test2.cpp:14:0:
/usr/include/c++/7/functional: In instantiation of ‘struct std::_Bind_check_arity<double (Example::*)(double, double, double), Example*>’:
/usr/include/c++/7/functional:854:12: required from ‘struct std::_Bind_helper<false, double (Example::*)(double, double, double), Example*>’
/usr/include/c++/7/functional:875:5: required by substitution of ‘template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...) [with _Func = double (Example::*)(double, double, double); _BoundArgs = {Example*}]’
test2.cpp:29:25: required from ‘std::function<double(Args ...)> Example::getSomeFun() [with Args = {double, double, double}]’
test2.cpp:36:40: required from here
/usr/include/c++/7/functional:841:7: error: static assertion failed: Wrong number of arguments for pointer-to-member
static_assert(_Varargs::value
^~~~~~~~~~~~~
test2.cpp: In instantiation of ‘std::function<double(Args ...)> Example::getSomeFun() [with Args = {double, double, double}]’:
test2.cpp:36:40: required from here
test2.cpp:29:58: error: could not convert ‘std::bind(_Func&&, _BoundArgs&& ...) [with _Func = double (Example::*)(double, double, double); _BoundArgs = {Example*}; typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type = std::_Bind<double (Example::*(Example*))(double, double, double)>](((Example*)this))’ from ‘std::_Bind_helper<false, double (Example::*)(double, double, double), Example*>::type {aka std::_Bind<double (Example::*(Example*))(double, double, double)>}’ to ‘std::function<double(double, double, double)

return std::bind(&Example::getSome<Args...>, this);

最佳答案

对于绑定(bind),您需要放置占位符参数:

std::bind(&Example::getSome<Args...>, this,
std::placeholders::_1,
std::placeholders::_2,
std::placeholders::_3
);

您需要放置的占位符数量等于您的函数需要的参数数量。

我建议在这种情况下使用 lambda,因为可变占位符不是一回事:

template<typename... Args>
std::function<double(Args...)> getSomeFun() {
return [this](Args... args) {
return getSome(args...);
};
}

此外,如果您可以在调用站点努力构造 std::function,则可以使用推导的函数返回类型来摆脱 std::function 的开销你调用它的地方不需要 std::function:

template<typename... Args>
auto getSomeFun() {
return [this](Args... args) {
return getSome(args...);
};
}

关于c++ - 如何返回使用参数包定义的成员函数的 std::function 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58881891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com