The problem is the following, in C++14
:
FV&& valid_f
, FI&& invalid_f
, and argu
Piotr Skotnicki's answer is superb, but code like that makes me feel compelled to point out how much cleaner C++17 will be thanks to constexpr if
and additional type traits like is_callable
: Demo Demo*This version creates more warnings but is simpler
template
void apply_on_validity(FV&& valid_f, FI&& invalid_f, Args&&... args)
{
if constexpr (std::is_callable_v)
std::cout << "Apply valid_f by default\n";
else
{
if constexpr (std::is_callable_v)
std::cout << "Apply invalid_f if valid_f does not work\n";
else
std::cout << "Do nothing when neither valid_f nor invalid_f work\n";
}
}