Or distribute the declarations the opposite way to create the opposite
(but more easily non circular) dependency:
Put the line
template<> const char* getstring<foo::bar>();
In the header that defines foo and declares (and maybe defines) bar, rather than in a header dedicated to getstring.
That does mean you need the declaration
template<typename T> const char* getString();
in lots of places that shouldn't need to know that getstring exists. But that tends to be less messy than defining foo in lots of places that shouldn't really need to know that foo::bar exists.
Adam McLaurin wrote:
As I said in another email, this is fine so long as the specialization
is for a primitive type. For user-defined types it gets much more
complicated, and leads to circular dependencies.
On Fri, 10 Jul 2009 09:25 -0400, "John Fine" <johnsfine@xxxxxxxxxxx>
wrote:
No. The header file did not declare the specialization, so the
specialization was called without being declared.
I think you only need to declare it, not define it, in the compilation
where it is called (though one of the posts in that link said
otherwise). Change the header file to
// Binky.h
#ifndef BINKY_H
#define BINKY_H
template<typename T> const char* getString()
{
return "T";
}
template<> const char* getstring<int>();
#endif