Original Post
Hi I am trying to explicitly instantiate (is that the right way to say it?) a template inside a namespace. The above does not compile (in g++), but it is intended to. foo() is a template function that I want to explicitly instantiate for (in this example) int. bar() is a template function that calls the template function foo(). the function foo() is declared and defined with template parameter T as "int". bar() is called with template parameter T as "int", calling foo() also with T as "int". The error I get is: demo.cpp:(.gnu.linkonce.t._Z3barIiEvT_[void bar(int)]+0x10): undefined reference to `void zug::foo(int&)' If I remove the namespacing, it compiles fine. It also compiles fine if I declare the int version of foo before the template version. Can anyone see what is wrong with my syntax? Am I supposed to indicate somehow that my explicit instantiation of foo() is a template instantiation? All help appreciated.
// INSIDE GENERIC HEADER:
namespace zug {
template <typename T> void foo(T &var);
}
template <typename T>
inline void bar(T var) {
zug::foo(var);
}
// INSIDE OBJECT-SPECIFIC HEADER:
namespace zug {
void foo(int &var);
}
// INSIDE OBJECT-SPECIFIC SOURCE:
void zug::foo(int &var) {}
// MAIN ROUTINE:
int main() {
bar(1);
return 0;
}