I have two overloads of operator(), one that takes a function reference that takes any type as its parameters and returns any type. And another one which takes a function reference that takes any type as its parameter but returns void. Upon instantiation of my class I get the following errors:
In instantiation of 'A<void, int>':
error: 'void A<T, F>::operator()(void (&)(F)) [with T = void, F = int]' cannot be overloaded
error: with 'void A<T, F>::operator()(T (&)(F)) [with T = void, F = int]'
template <typename T, typename F> struct A {
void operator()(T (&)(F)) {}
void operator()(void (&)(F)) {}
};
void f(int) {}
int main() {
A<void, int> a;
a(f);
}
These errors only occur when the first template argument T is void. I would like to know what I’m doing wrong and why I can’t overload operator() this way?
Well, if
Tisvoidthen you have two function definitions with the exact same prototype – breaking ODR.Try specializing your struct to prevent this: