Assume the following two functions may or may not be provided by the user:
void foo(int) { std::cout << "foo int" << std::endl; }
void foo() { std::cout << "foo void" << std::endl; }
In my implementation, I want to call foo(int)
if the user defined it, and foo()
otherwise. This can be done as follows:
template<class Int> auto call_foo(Int i) -> decltype(foo(i)) { return foo(i); }
template<class... Int> void call_foo(Int... i) { return foo(); }
If, however, we want to do the opposite, i.e. prefer foo()
over foo(int)
, the following naive attempt does not work.
template<class Int> auto call_foo(Int i) -> decltype(foo()) { return foo(); }
template<class... Int> void call_foo(Int... i) { return foo(i...); }
The problem is that decltype(foo())
does not depend on Int
, so the possible non-existence of foo()
does not result in SFINAE.
A possible solution is to require the user to define either of
void foo(int, void*) { std::cout << "foo int" << std::endl; }
void foo(void*) { std::cout << "foo void" << std::endl; }
Like this, we always have a parameter of foo
on which we can do the "template vs. parameter-pack" trick. While this technically solves the problem, it's quite ugly as it requires the user to take an additional parameter whose meaning might not be obvious to him/her. So is there a way to achieve the same effect without the additional parameter?