我需要一种方法将
std::bind()
返回的函数对象分解为其函数模板中的参数。
下面的代码片段显示了我想要做的事情:
#include <iostream>
#include <functional>
void foo(int x, double y, char z)
{
std::cout << "x = " << x << ", y = " << y << ", z = " << z << '\n';
}
int main()
{
auto f = std::bind(foo, 42, 3.14, 'a');
std::cout << "The first argument is: " << std::get<0>(f) << '\n';
std::cout << "The second argument is: " << std::get<1>(f) << '\n';
std::cout << "The third argument is: " << std::get<2>(f) << '\n';
}
输出应该是:
The first argument is: 42
The second argument is: 3.14
The third argument is: a
但是它没有编译并出现错误:
test.cpp:12:60: error: no matching function for call to ‘get<0>(std::_Bind<void (*(int, double, char))(int, double, char)>&)’
如有任何帮助,我们将不胜感激。
这是不可能的。
std::bind
返回未指定的类型,仅保证以下内容:
result_type
类型别名;operator()
.如果您需要问题中描述的功能,您必须提出自己的
bind
定义。