explicit template instantiations

Posted by user323422 on Stack Overflow See other posts from Stack Overflow or by user323422
Published on 2010-04-29T07:17:34Z Indexed on 2010/04/29 9:27 UTC
Read the original article Hit count: 345

Filed under:
|

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 
} 

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates