用C++传递带有参数的函数
Pass a function with the arguments in C++
在C++中是否可以将函数的参数作为参数传递?我不想只传递函数名。我想传递包含参数的函数名。请注意,我不知道函数接收了多少参数,也不知道类型。这就是为什么我不将它们作为额外参数添加。我只想引用一个可以按需执行的函数调用。例如:
CallFunction(f(param1, param2));
但是f没有被评估,我只是得到了对函数调用的引用,我可以在需要的时候评估它,例如:
CallFunction(f){
//do something and then in one moment do:
f; // and this calls f(param1, param2)
}
有什么方法可以在C++中实现这一点?提前谢谢。
使用std::bind
:的完整示例
#include <iostream>
#include <functional>
void f(int p1, int p2) {
std::cout << "In f(" << p1 << "," << p2 << ")n";
}
template <typename F>
void CallFunction(F f)
{
std::cout << "Start CallFunctionn";
f();
std::cout << "End CallFunctionn";
}
int main()
{
CallFunction(std::bind(f, 2, 3));
}
您有几个选项。
一种是创建一个包装器闭包,它将封装对函数的调用。这样做的优点是,如果需要,可以转换函数的输出。
做一个简单的包装器和前面提到的使用std::bind
是一样的。
#include <functional>
#include <iostream>
void CallFunction(std::function<void(void)> f) {
f();
}
int main() {
auto closure = [](int a, int b) {
std::cout << a << ", " << b << std::endl;
};
auto wrapper = [closure]() {
closure(1, 2);
};
CallFunction(wrapper);
CallFunction(std::bind(closure, 3, 4));
return 0;
}
- 接受任何可调用函数作为第一个参数
- 使用可变模板并接受任意数量的任何类型的参数
- 使用完美的转发
http://ideone.com/z2kLUx
int HelloWorld(string var1, const char* var2)
{
cout << var1 << " " << var2 << endl;
return 5;
}
template<typename Fn, typename... Args>
auto CallFunction(Fn fn, Args&&... args)
{
return fn(forward<Args>(args)...);
}
int main()
{
cout << "Value returned: " << CallFunction(HelloWorld,"Hello","World");
return 0;
}
这是一个简单的版本,它有一个缺点:您必须指定一个返回非void的函数,但这可以很容易地解决。简单的解决方法是创建另一个用不同名称返回void的CallFunction,或者使用std::enable_if
。
http://ideone.com/El3WUi
template<typename Fn, typename... Args>
auto CallFunction(Fn fn, Args&&... args) -> typename enable_if<is_same<void, decay_t<decltype(fn(forward<Args>(args)...))>>::value, decltype(fn(forward<Args>(args)...))>::type
{
fn(forward<Args>(args)...);
}
template<typename Fn, typename... Args>
auto CallFunction(Fn fn, Args&&... args) -> typename enable_if<!is_same<void, decay_t<decltype(fn(forward<Args>(args)...))>>::value, decltype(fn(forward<Args>(args)...))>::type
{
return fn(forward<Args>(args)...);
}
相关文章:
- 将可变参数函数的参数封装在类实例中
- QML 使用带有参数C++函数
- 使用可变参数函数作为模板参数
- 如何在C++中伪造虚拟可变参数函数模板?
- 为什么可变参数函数不适用于模板
- C++ std::functional 中的可变参数函数模板
- 可变参数函数指针的定义对于VxWorks spyLib来说不清楚
- 使用可变参数函数覆盖具有不同函数签名的虚函数
- 考虑引用和常量的可变参数函数包装器
- 使用可变参数函数将整数和/或整数数组放入单个 int 数组中
- 在可变参数函数中转发特定范围的参数
- 通过引用传递参数;函数返回类型是否必须为 VOID?
- 使用带有一个参数函数的递归找到数字的平方
- 可变参数函数模板不能很好地使用 std::function 作为参数
- 多个可变参数函数的单个模板参数包?
- 参数数据类型未知的可变参数函数
- 可变参数函数参数包扩展
- 使用模板可变参数函数将多个参数传递给另一个函数
- 对可变参数函数的递归调用的链接器错误
- 通过像printf这样的可变参数函数传递一个带有常量字符*转换函数的类