¿Cómo puedo evitar la generación de plantillas para objetos que no implementan un método?
Entonces, a modo de ejemplo, digamos que tengo 3 simplesstruct
s, el segundo de los cuales no contiene unbar
método:
struct one {
void foo(const int);
void bar();
};
struct two {
void foo(const int);
};
struct three {
void foo(const int);
void bar();
};
Entonces tengo unstruct
que gestionará objetos de estos tipos:
struct owner {
map<int, one> ones;
map<int, two> twos;
map<int, three> threes;
template <typename T, typename Func>
void callFunc(T& param, const Func& func) {
func(param);
}
template <typename T>
void findObject(int key, const T& func) {
if(ones.count(key) != 0U) {
callFunc(ones[key], func);
} else if(twos.count(key) != 0U) {
callFunc(twos[key], func);
} else {
callFunc(threes[key], func);
}
}
void foo(const int key, const int param) { findObject(key, [&](auto& value) { value.foo(param); } ); }
void bar(const int key) { findObject(key, [&](auto& value) { value.bar(); } ); }
};
Cuando intento compilar esto obtengo:
error:struct two
no tiene miembro nombradobar
¿Hay alguna manera de evitar esto?