简体   繁体   English

C ++函数调用

[英]C++ function call

Imagine function func that needs var during run time. 想象一下在运行期间需要var功能func Is it possible under c++ to call func without var as a parameter? 在c ++下是否可以在不使用var作为参数的情况下调用func That is, to have something like this 也就是说,拥有这样的东西

func(var, i) = func(i);

I would like to do so for the sake of simple readability. 我希望这样做是为了简化可读性。 Inside my class I could set var as a global parameter and then call only func(i) but this would give troubles if I was going to use threads... 在我的类内部,我可以将var设置为全局参数,然后仅调用func(i)但是如果我要使用线程,这会带来麻烦。

But note that func uses var during run time. 但请注意, func在运行时使用var Ex: 例如:

int func(int var , int i){
return i+var;
}

In fact I will have multiple calls of func(), but all thes calls share the same var . 实际上,我将多次调用func(),但是所有这些调用都共享同一个var Ex: instead of 例如:代替

int var=2;
int res=   func(var, 0) + func(var, 1) +..... func(var, n);

I would write more concisely (in it is the same var ) 我会写得更简洁(因为它是相同的var

int var=2;
int res=   func(0) + func(1) +..... func(n);

You can do this using lambdas: 您可以使用lambdas执行此操作:

auto bound_func = [var](auto i) { return func(var, i); }

Or you can have more freedom by just storing the var in a struct: 或者,您可以通过将var存储在结构中来获得更大的自由度:

struct func_binder
{
    var v_;

    func_binder(var v)
      : v_(v)
    { }

    decltype(func(v_, i)) operator()(int i)
    {
        return func(v_, i);
    }
};

This can be used like: 可以这样使用:

func_binder f(some_var);
f(some_i);

If var is isn't mutated by the function, then you don't need to worry about it with threads. 如果var不被函数突变,那么您不必担心线程。 If it is, then it should be wrapped in a structure that that synchronizes access to it somehow. 如果是这样,则应将其包装在以某种方式同步对其访问的结构中。

怎么样?:

auto func_binded = std::bind( func, var, std::placeholders::_1 );

Add a new function with the same name that takes the parameter i. 添加具有参数i的同名新函数。 This function can call the other function with The correct var. 该函数可以使用正确的var调用另一个函数。 This is called function overloading. 这称为函数重载。

func(i){
  func(global_var, i);
}

yes you can use a default value for you var variable declare the prototype like this: 是的,您可以为您的变量使用默认值,这样声明原型:

fun(type_of_i i,typeofvar var=default_value);

try to use the most relevant value for the default value Of course you must be able to permute the order of parameters, otherwise use the function overloading mechanism as said in a previous answer regards 尝试使用最相关的值作为默认值当然,您必须能够排列参数的顺序,否则请使用前面的回答中所述的函数重载机制

When declaring your function, you can specifiy a default argument. 声明函数时,可以指定默认参数。 In your case it has to be the last parameter of your function. 在您的情况下,它必须是函数的最后一个参数。

void func(int i, int var=0);

then call it like that func(i); 然后像func(i);这样称呼它func(i);

which is func(i, 0); 这是func(i, 0);

So, you have at least two solutions, the first one is simple to add to a class : 因此,您至少有两个解决方案,第一个很容易添加到类中:

  • Use variadic template to build a wrapper function (my preferred solution in your case, easy to add to a class). 使用可变参数模板来构建包装函数(在您的情况下,我的首选解决方案是易于添加到类中)。 This solution allows you to write int r = newfunc(var, 1, 2, 3); 该解决方案允许您编写int r = newfunc(var, 1, 2, 3); instead of repeating again and again function name : 而不是一次又一次地重复函数名:

     int funcvar(int var) { return 0; } template<typename TFirst, typename ... T> int funcvar(int var, TFirst first, T ... args) { return func(var, first) + funcvar(var, args...); } 
  • Use lambda (a little be difficult to add to a class, because you need a variable of type like std::function<int(int)> fvl and initialized in constructor or pass it by argument): 使用lambda(很难添加到类中,因为您需要一个类型为std::function<int(int)> fvl并在构造函数中std::function<int(int)> fvl进行初始化或通过参数将其传递):

     auto fvl = [var](int i) { return func(var, i); }; 

examples : 例子 :

int main()
{
    int var = 1;

    // Use of variadic template
    int result1 = funcvar(var, 1, 2, 3, 4, 5);
    int result2 = funcvar(var, 5, 6);

    // Use of lambda
    auto fvl = [var](int i) { return func(var, i); };
    int result3 = fvl(1) + fvl(2) + fvl(3) + fvl(4) + fvl(5);
    int result4 = fvl(5) + fvl(6);

    std::cout << result1 << std::endl;
    std::cout << result2 << std::endl;
    std::cout << result3 << std::endl;
    std::cout << result4 << std::endl;
}

see here 这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM