mercredi 6 mai 2015

Force function parameter to match some rule

Is there any way to "force" a function parameter to follow some rule in C++ ?
For the sake of example, let say I want to write a function which computes the n'th derivative of a mathematical function. Let suppose the signature of the function is this one :

double computeNthDerivative(double x, unsigned int n);

Now, let say I want to forbid users to input 0 for n. I could just use an assert or test the value and throw an exception if the user input is 0.
But is there any other way of doing this kind of stuff ?

Aucun commentaire:

Enregistrer un commentaire