This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: problems with template instantiation


John McCurry <john.mccurry@talk21.com> writes:

| Hi folks,
| 
| I expect this to be a trivial problem for someone who has more
| experience with the GNU compiler than I do.
| The problem is that I can't get the compiler to automatically create the
| required instantiations of template
| classes (which I think it should do). I hope the following example will
| illustrate the point:
| 
| I have built a trivial application from the 3 files: a.h, a.cxx and
| main.cxx
| 
| FILE a.h:
| 
|     template <class T> class A
|     {
|     public:
|         A();
|         A(const T& t);
|         ~A();
|         T getValue() const;
| 
|     private:
|         T value;
|     };
| 
| <EOF>
| 
| 
| FILE a.cxx:
| 
|     #include "a.h"
| 
|     template <class T> A<T>::A() {}
|     template <class T> A<T>::A(const T& v) { value = v; }
|     template <class T> A<T>::~A() {}
|     template <class T> T A<T>::getValue() const { return value; }
| 
| <EOF>
| 
| 
| FILE main.cxx:
| 
|     #include <iostream.h>
|     #include "a.h"

That can't work.  The "standard" way of making it to work is through
use of the keyword "export".  However, G++ doesn't understand export;
so you have to make your template definitions visible to the compiler
whenever they get instantiated (for example by #including a.cxx at the
end of a.h).

Please, notice that this list is about GNU compilers development;
gcc-bugs is for bugs found in GCC.  Any C++ specific question is to be
sent to dedicated forums.

-- Gaby
CodeSourcery, LLC                             http://www.codesourcery.com

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]