Advertisement

Template Fun

Started by May 30, 2000 06:40 PM
14 comments, last by Josh Neta 24 years, 6 months ago
Does anyone know if in VC++6 there is a way to have a template class definition in a header and the actual implimentation in a .cpp file? When i try doing it everything compiles but i get an unresolved external when linking unless i put everything in the header (which i dont want). Anyone know if i need to set some pragma or something (i know in gcc you need #pragma interface and implimentation but those aren''t in VC, well interface is but it''s for something else)? Thanks Josh
I''m not sure if you can force the separation compilation model in VC++6. However, you can put the actual implementation in a CPP file and then include that .CPP file in your header file. It still leaves you with a large header file, but if you use precompiled headers, compilation can be sped up (assuming that the header file with the template definition and included implementation do not change often).

Erik
Advertisement
Hmm... Not liking that solution much but thanks for the quick replay.

Josh
I haven't done it myself, and it's not supported very well (as far as I know) in vc++6, but in the standard, this is how to do it:

//mytemplates.h
template<class T> void myfunction(T *t);

//mytemplates.cpp
#include "mytemplates.h"
export template<class T> void myfunction(T *t) {
//dostuff_here
}

It's the "export" keyword that does it.



Edited by - Armitage on May 31, 2000 9:06:07 AM
A polar bear is a rectangular bear after a coordinate transform.
Well for templates, anyone using them has to know what the implementation is(what''s usually in the .cpp file) So you have to put that in the header file or export it like Armitage said.

Eck

EckTech Games - Games and Unity Assets I'm working on
Still Flying - My GameDev journal
The Shilwulf Dynasty - Campaign notes for my Rogue Trader RPG

Armitage: The export keyword is totally unrelated to what you are trying to do with templates. It will generate a warning in VC 6 and will not do anything for you. It is an obsolete keyword.



- null_pointer
Sabre Multimedia
Advertisement
The export is definately related to templates, and is by no means obsolete. The problem with export is that not many compilers support it.

An exported template is a template for which only the template definition is required for the template instantiations to be used. That way, you can put your template definitions in a header file, and put the template declaration in a source file. Code that uses the template only have to include the header file, without having to include the declarations themselves.

Erik
Erik Post: Here''s the warning I get from VC 6:

quote: VC Compiler Warning

nonstandard extension used : ''keyword'' is an obsolete keyword, see documentation for __declspec(dllexport)

You used the _export keyword, which is now obsolete. Use __declspec(dllexport) instead.


That''s an old VC extension that is completely unrelated to templates. As you can see, it was used to add DLL functions and data to the list of things to be exported without the use of a .DEF file. If the export keyword (a different definition) has been recently added to the standard, then I concede.

I revise my old opinion -- VC has lousy ANSI C++ support. BTW, do you know of a Windows compiler/IDE that works better with ANSI C++?



- null_pointer
Sabre Multimedia
That VC++ warning talks about a keyword ''_export''. The C++ keyword that we''re talking about is ''export''. Maybe there''s a define somewhere that changes the definition of export to _export? Btw, the piece of info I wrote about export came right out of my C++ book which reflects the ANSI/ISO standard.

As for a better compiler, I personally use the free Borland C++ 5.5 commandline utilities (it normally comes with C++Builder 5). The compiler is pretty good, and supports most of the ANSI/ISO standard from what I can tell. Export isn''t supported, unfortunately It comes with the Rogue Wave version of the STL and IOstreams lib, as well as most of the Windows header files. The download is on http://www.borland.com/.

Another downside is that it lacks a good IDE. As for writing source code, I don''t find that a real problem since I''ve got a pretty decent text editor. But it takes a bit of effort to find out the correct compiler switches. And, the greatest downside: there''s no debugger. Since Borland uses a different object/debug file format, you can''t use Visual Studio''s IDE. If you can get C++Builder 5, you will get a good IDE and debugger.

Erik

PS. If you start to use BCC 5.5, please note that there''s a bug in the string version of getline which is quite annoying, but simple to fix.
null, I said I hadn''t tried it - and I read in "deep c++" that no compiler what so ever supported it good
But the code was basically taken from Stroustrups c++ the prog.., so it''s not obsolete.

Damn I hope visual studio 7 is good. Has anyone tried it?
A polar bear is a rectangular bear after a coordinate transform.

This topic is closed to new replies.

Advertisement