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 T
is void
then you have two function definitions with the exact same prototype - breaking ODR.
Try specializing your struct to prevent this:
template <typename T, typename F> struct A {
void operator()(T (&)(F)) {}
void operator()(void (&)(F)) {}
};
template <typename F> struct A<void, F> {
void operator()(void (&)(F)) {}
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.