0

I wish to be able to manipulate function arguments by the order in which they are given. So,

void sum(int a, int b, int c)
  std::cout<< arguments[0]+arguments[1];
sum(1,1,4);    

should print 2. This feature is in JavaScript.

I need it to implement a numerical scheme. I'd create a function that takes 4 corner values and tangential direction as input. Then, using the tangential direction, it decides which corners to use. I wish to avoid an 'if' condition as this function would be called several times.

EDIT - The reason why I do not wish to use an array as input is for potential optimization and readability reasons. I would explain my situation a bit more. solution is a 2D array. We would be running this double for loop several times

for (int i = 0;i<N_x;i++)
  for (int j = 0;j<N_y;j++)
    update_solution(solution[i,j],solution[i+1,j],solution[i-1,j],...);

Optimization: N_x,N_y are large enough for me to be concerned about whether or not adding a step like variables_used = {solution(i,j),solution(i+1,j),...} in every single loop will increase the cost.

Readability The arguments of update_solution indicate which indices were used to update the solution. Putting that in the previous line is slightly non-standard, judging by the codes I have read.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Lelouch
  • 101
  • 2
  • Take `array` as argument? – Jarod42 Jan 09 '21 at 10:57
  • @Jarod42 What if arguments are of different data types? – Adnan Ahmed Jan 09 '21 at 11:00
  • 1
    @AdnanAhmed Then use a tuple. – super Jan 09 '21 at 11:01
  • 1
    @super just lookedup tuple iteration and its not easy to do especially considering different versions of c++. Not recommended for simple tasks like this one but if necessary, here is a list of iteration techniques for tuples: https://stackoverflow.com/questions/1198260/how-can-you-iterate-over-the-elements-of-an-stdtuple – Adnan Ahmed Jan 09 '21 at 11:22
  • I have updated my question to explain why I wished to avoid taking arrays as input. In the process, I learnt that taking vectors as input does solve my problem. I can write something like `update_solution({solution[i,j],solution[i+1,j],...})`. Thank you very much! – Lelouch Jan 09 '21 at 11:37
  • 1
    `std::vector` would have more overhead that `std::array` logically for your usage. – Jarod42 Jan 09 '21 at 12:00
  • 1
    Java emphatically does *not* have this feature — what you’ve linked is Java*Script*, a completely different, and *completely unrelated* language. In C++, as in Java, you implement this by passing a collection (or iterators). – Konrad Rudolph Jan 09 '21 at 12:03

0 Answers0