如何用参数值调用函数(仅在运行时已知)

How to call a function(only known at runtime) with parameter values(only known at runtime)?

本文关键字:运行时 何用 参数 值调用 函数      更新时间:2023-10-16

在运行时进行一次测试,以确定要调用两个函数中的哪一个,并确定参数值。

然后每隔几秒钟调用一次该函数。

void Foo::func( std::string s);
void Foo::func( std::string s1 std::string s2, std::string s2);

显然,每次调用测试(调用哪个函数(效率很低,尤其是当函数及其参数值一旦建立就不会改变时。

我需要这样的东西:

测试一次要调用的函数和已建立的参数值。

将初始化的函数分配给某种可调用变量。

funcVar = void Foo::func( "blah", "blah", "woof" );

funcVar = void Foo::func( "blah" );

然后调用函数;

for( every few seconds )
{
call FuncVar;
}

有什么可调用的变量可以用来做这件事吗?

您可以简单地使用lambda函数对象(闭包(,您可以随时调用它。

示例:

struct Foo 
{
void func( std::string s){ std::cout << "single" << s << std::endl; };
void func( std::string s1, std::string s2, std::string s3) 
{   
std::cout << "tripple " << s1 << " " << s2 << " " << s3 << std::endl;
}   
// Select which function to call
std::function<void()> Select( int what )
{   
if ( what == 1 )
{   
// Create a callable object and put in also parameters
return [this]() { func("Hallo"); };
}
else
{   
// The same for the other selection
return [this]() { func("This","is","tripple!"); };
}
}   
};  
int main()
{   
Foo foo;

// Pick what you want to select
auto action = foo.Select( 2 );
for ( ;; )
{   
// and call it as often you like
action();
}   
}

您可以使用std::bind为两种用例创建一个具有存储参数和公共调用运算符的函子。检查这个片段:

#include <string>
#include <functional>
struct Foo {
static void a(std::string) {}
static void b(std::string, std::string) {}
};
int main() {
std::function<void()> x;
if(true) {
x = std::bind(&Foo::b, "a", "b");
}
x();
return 0;
}

如果要std::绑定重载函数,必须指定要使用的函数:

#include <string>
#include <functional>
#include <iostream>
#include <map>

struct Foo {
void b() { std::cout << name << ": function 1" << std::endl; }
void b(int i) {std::cout << name << ": function 2" << std::endl; }
std::string name;
};

void with_map() {
Foo f;
f.name = "with_map";
std::map<const int, std::function<void()>> map({
{10, std::bind<void(Foo::*)(), Foo&>(&Foo::b, f)},
{20, std::bind<void(Foo::*)(int), Foo&, int>(&Foo::b, f, 1)},
});
map[20]();
map[10]();
}

void without_map() {
Foo f;
f.name = "without_map";
std::function<void()> x = std::bind<void(Foo::*)(), Foo&>(&Foo::b, f);
x();
x = std::bind<void(Foo::*)(int), Foo&, int>(&Foo::b, f, 1);
x();
}

int main() {
without_map();
with_map();
return 0;
}

std::函数应该在这里工作。

std:: function<void()> fun = [&s] () {/* do  work*/};
if (some_condition)
{
fun = [&s1,&s2,&s3] () {/* do your work*/} ;
}
if(every few seconds)
{
fun();
}