Consider the following code:
#include <utility>
#include <iostream>
struct S {
template<typename T, typename... A>
auto f(A&&... args) -> decltype(std::declval<T>().f(std::forward<A>(args)...), void()) {
std::cout << "has f(int)" << std::endl;
}
template<typename>
void f(...) {
std::cout << "has not f(int)" << std::endl;
}
};
struct T { void f(int) { } };
struct U { };
int main() {
S s;
s.f<T>(42);
s.f<U>(42);
s.f<T>();
}
As shown in the example, the third call fworks just fine, even if the number of arguments is incorrect, since it is not an error at all for the backup function.
Is there a way to force the number of arguments to use this ellipsis?
I mean, can I check at compile time that the size of the argument list is exactly equal 1, regardless of whether the main function or the backup is selected?
Good solutions are also those that include only the first function of the template and lead to errors with errors instead of soft errors due to the size of the parameter packet.
, . : int/char ; ; ...
, .
, - .