This previous answer shows how to apply function based on the validity of a call: here. However, it applies to two functions. I was wondering if the concept could be generalized to N
functions using smart template programming tricks, in C++14.
The problem is the following:
template <std::size_t N, class... X>
/* [Return type] */ apply_on_validity(X&&... x)
{
// Tricks here
}
// The function would be equivalent to a hypothetical
// template <std::size_t N, class... F, class... Args>
// [Return type] apply_on_validity(F&&... f, Args&&... args)
// where N = sizeof...(F) is the number of functions
On the execution side:
apply_on_validity<3>(f1, f2, f3, a, b, c, d, e);
Would:
- call
f1(a, b, c, d, e)
if the expression is valid, otherwise - call
f2(a, b, c, d, e)
if the expression is valid, otherwise - call
f3(a, b, c, d, e)
if the expression is valid, otherwise - do nothing
And in all cases, it would return the result of the function that was executed.
On the calling side, the template parameter 3
indicates the number of functions that have been specified.
Is it doable in C++14, and if so, how?
See Question&Answers more detail:os