Image a function will return same output if input are same. To reduce redundant call, you may want a class can memorize previous return value and return it to you. No_redundant_call is used for this purpose.
No_redundant_call will compare arguments you pass to all previous function calls. If No_redundant_call can find an exactly match, No_redundant_call will return the previous return value (if it is not void type). Otherwise, No_redundant_call will invoke the function and store the return value so that following function calls does not have to invoke the function again (if arguments are same).
If your function has side effect, No_redundant_call cannot help you.
See Test.
In a function below,
void store_to_arg(int &i)
{
i = 10;
}
No_redundant_call cannot know reference type is used for output (int &i
). As a result, the following code will make No_redundant_call strange but correct.
nTool::No_redundant_call no_redundant;
int i(0);
no_redundant.invoke(store_to_arg, i);
cout << i << endl; // 10
i = 0;
no_redundant.invoke(store_to_arg, i);
cout << i << endl; // 0, seriously?
i = 1;
no_redundant.invoke(store_to_arg, i);
cout << i << endl; // 10
I also demonstrate another solution (sample). Hope it can help you.