I have a template class which takes on roughly the form of the given code below.
template <int index = 0>
struct Thing {
void Hello();
void Greet(const char *name);
};
Which works just fine for its purpose, until one decides to try and call methods on itself. Say that you define the methods as shown below.
template <>
void Thing<>::Greet(const char *name) {
Hello();
printf(", %s!\n", name);
}
template <>
void Thing<>::Hello() {
printf("Hello");
}
Then calling Greet()
yields an explicit specialisation error, which I find weird. The compiler ought to know all of the methods of this
by its interface declaration, but for some reason it cannot resolve it in this case. This can be solved in one of two ways; either you must forward-declare any methods called on yourself, or make sure the methods are defined in an order that ensures that the ones you call are defined beforehand.
This issue is really annoying, because I have provided an interface declaration, and any specialisation of the template ought to conform to that same interface, so I really don't see why the compiler complains about this — is there any way to fix the issue without polluting the code with forward-declarations of each method, or having to order methods in a particular manner?
I hope you have some great ideas for solving the issue in a better way. Thank you in advance!
For ease of reproducing the issue, here's a snippet that will call the offending method.
int main(int argc, const char *argv[]) {
Thing<0> thing_with_zero;
thing_with_zero.Greet("World");
return 0;
}