Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to guard against function arguments being passed in the wrong order?

Say I have a C++ function that looks like this:

double myfunction(double a, double b) {
    // do something
}

Which I then call like this:

double a = 1.0;
double b = 2.0;
double good_r = myfunction(a, b);
double bad_r = myfunction(b, a); // compiles fine

I would like to make sure that a and b are never provided in the wrong order. What is the best way to ensure this in C++?

Other languages allow named parameters, like this:

double good_r = myfunction(a=a, b=b);
double bad_r = myfunction(a=b, b=a); // mistake immediately obvious
double bad_r = myfunction(b=b, a=a); // compiles fine

Or perhaps the problem can be partly solved using types, i.e.

double my_type_safe_function(a_type a, b_type b) {
    // do something
}
a_type a = 1.0;
b_type b = 2.0;
double good_r = myfunction(a, b);
double bad_r = myfunction(b, a); // compilation error

EDIT: A couple of people have asked what I mean by the "wrong order." What I mean is that, in real code a and b have some significance. For example, the arguments might instead be height and width. The difference between them is very important for the function to return the correct result. However, they are both floats and they both have the same dimensions (i.e. a length). Also, there is no "obvious" order for them. The person writing the function declaration may assume (width, height) and the person using the function may assume (height, width). I would like a way to ensure this doesn't happen by mistake. With two parameters it is easy to be careful with the order, but in a large project and with up to 6 arguments mistakes creep in.

Ideally I would like the checks to be done at compile time, and for there to be no performance hit (i.e. at the end of the day they are treated as plain old floats or whatever).

like image 496
user2664470 Avatar asked Apr 23 '14 12:04

user2664470


People also ask

Does the order of arguments in a function matter?

Argument Order MattersThe order or arguments supplied to a function matters. R has three ways that arguments supplied by you are matched to the formal arguments of the function definition: By complete name: i.e. you type main = "" and R matches main to the argument called main .

When calling a function the arguments should be in order?

3. Positional Arguments. During a function call, values passed through arguments should be in the order of parameters in the function definition. This is called positional arguments.

How are function arguments passed?

Arguments are passed by value; that is, when a function is called, the parameter receives a copy of the argument's value, not its address. This rule applies to all scalar values, structures, and unions passed as arguments. Modifying a parameter does not modify the corresponding argument passed by the function call.


1 Answers

How about this:

struct typeAB {float a; float b; };

double myfunction(typeAB p) {
// do something
  return p.a - p.b;
}

int main()
{
  typeAB param;
  param.a = 1.0;
  param.b = 2.0;
  float result = myfunction(param);
  return 0;
}

Of course, you can still mess up when you assign your parameter(s) but that risk is hard to avoid :)

like image 130
Martin G Avatar answered Sep 30 '22 06:09

Martin G