I try to create a framework that allows developers to write handlers on different levels of abstraction. Therefore I try to write a base handler, that has the less abstract interface and calls the derived handler with a more abstract interface and the default operations like e.g. this:
template<typename Derived>
struct base {
template<typename A>
int operator () (int x, A& a) {
return (*reinterpret_cast<Derived*>(this))(x) + a;
}
};
The derived handler should only have to implement the simple interface:
struct derived: public base<derived> {
int operator () (int x) { return x; }
};
But when I use it like this:
int main(int, char**) {
derived d;
std::cout << d(1, 2) << std::endl;
}
I get the folowing output from g++ -c -Wall -Werror test2.cpp
(gcc version is 5.3.1):
test2.cpp: In function 'int main(int, char**)':
test2.cpp:18:24: error: no match for call to '(derived) (int, int)'
std::cout << d(1, 2) << std::endl;
^
test2.cpp:12:9: note: candidate: int derived::operator()(int)
int operator () (int x) { return x; }
^
test2.cpp:12:9: note: candidate expects 1 argument, 2 provided
I also tried with clang vresion 3.8.0 (clang++ -c -Wall -Werror test2.cpp
) with a similar result:
test2.cpp:18:18: error: no matching function for call to object of type 'derived'
std::cout << d(1, 2) << std::endl;
^
test2.cpp:12:9: note: candidate function not viable: requires single argument 'x', but 2 arguments were provided
int operator () (int x) { return x; }
^
1 error generated.
The call operator template in the base class is not considered. I have also tried to use a different method name, because maybe operators are somehow special, but that did not help as well. What can I do to create such and interface?
Of course I'd like to avoid to have to implement the call operator template with two operands in the derived class, because the derived class is, what the user of the framework is supposed to provide and I want to make his life as easy as possible.