explicit template instantiations
- by user323422
see following code and please clear doubts1. as ABC is template why it not showing error when we put defination of ABC class member function in test.cpp 2.if i put test.cpp code in test.h and remve 2 , then it working fine.
// test.h
template <typename T>
class ABC {
public:
void foo( T& );
void bar( T& );
};
// test.cpp
template <typename T>
void ABC<T>::foo( T& ) {} // definition
template <typename T>
void ABC<T>::bar( T& ) {} // definition
template void ABC<char>::foo( char & ); // 1
template class ABC<char>; // 2
// main.cpp
#include "test.h"
int main() {
ABC<char> a;
a.foo(); // valid with 1 or 2
a.bar(); // link error if only 1, valid with 2
}