Re: Template function is not found in object file after compiling with gcc 4.1.2‏

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Jian-ping
> I ran into a problem while building an old project with gcc 4.1.2. In object
> file, I could not find the expected symbol for template function. Here is an
> example program:
[I shortened the program by removing linebreaks]
> ========
> test.hpp
> ========
> class T { public: T(){}; ~T(){}; };
> template <class MyT> class test { public: int ftest(); };
>  
> ========
> test.cpp
> ========
> #include <test.hpp>
> template class test<T>;
> template <class MyT> int test<MyT>::ftest() { return 0; }
>  
> I ran the command
>     gcc  -c -I.  -o test.o test.cpp
> to compile test.cpp. In the object file "test.o", I could not find the
> symbol name "ftest" if I compiled test.cpp with gcc 4.1.2.
The problem is precise definition of explicite instantiation of
templates (14.7.2 in the C++-Standard): With 
template class test<T>;
you instantiate the class test<T>. But that does NOT mean, that you also
instantiate all member functions.
In order to instantiate the member function ftest, you have to write
instead or in addition:
template int test<T>::ftest();
If you do that, "test.o" will contain the symbol ftest.
(I wrote "instead", because as soon as you instantiate a member
function, the class itself will be instantiated too).

Well, at least that's my understanding of the situation - I'm not
absolutely sure; maybe that's one of the subtleness of Templates in C++
which I didn't understand correctly ;-)

Axel

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux