Re: [Dev-C++] Is it possible - templates (.h and .cpp) ?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Kevin B. <kev...@ma...> - 2004-05-01 04:28:17
|
>This makes it impossible to use templates without letting the >compiler see the full declarations for the template. Not entirely accurate. The C++ standard does, in fact, specify a facility for separate compilation of template code, using the export keyword. Unfortunately, there is almost no compiler support for this yet. In fact, to my knowledge only the EDG compiler supports it in any reasonably complete way. So, as a practical matter, it remains close to impossible for the immediate future. But, it is standard C++, and so eventually, anybody who wants to pretend to compliance will support it. Meanwhile, you can still divide your code into .h and .cpp, and merely use some conditional compilation to accomodate non-compliant compilers, something like so: // Global definition somewhere #if defined(USE_EXPORT) #define EXPORT export #else #define EXPORT #endif // mytemplate.h EXPORT template<class T> ...whatever... #if !defined(USE_EXPORT) #include mytemplate.cpp #endif In this way, you can divide your code, it'll work now, and you'll be ready for the future. Another option is to use explicit instatiation. This allows you to hide your implementation completely without all the preprocessor nonsense above, and still satisfy the linker by explicitly instatiating whatever various examples of the template your program happens to require. This can be a maintenance headache, though, since you need to keep track of what template variations are used by the rest of your program. /* mytemplate.cpp */ #include "mytemplate.h" // Implement template here. // Instatiate myclass for int and float, // which is all we use in the whole program. template myclass<int>; template myclass<float>; --- Kevin |