Courier
Courier

Reputation: 940

C++ function call

Imagine function func that needs var during run time. Is it possible under c++ to call func without var as a parameter? 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...

But note that func uses var during run time. 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. 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)

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

Upvotes: 0

Views: 123

Answers (6)

Garf365
Garf365

Reputation: 3707

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); 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):

    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

Upvotes: 0

claudio06
claudio06

Reputation: 44

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

Upvotes: 0

lorro
lorro

Reputation: 10880

How about?:

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

Upvotes: 2

Yuushi
Yuushi

Reputation: 26080

You can do this using 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:

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. If it is, then it should be wrapped in a structure that that synchronizes access to it somehow.

Upvotes: 2

Np3w
Np3w

Reputation: 81

Add a new function with the same name that takes the parameter i. This function can call the other function with The correct var. This is called function overloading.

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

Upvotes: 1

rkgghz
rkgghz

Reputation: 455

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);

which is func(i, 0);

Upvotes: 0

Related Questions