Visual C++ and templates

Started by
7 comments, last by jpetrie 18 years ago
Hi, I'm currently creating a particle system which uses the Policy parttern. Using this pattern implies that my particle system class is a templated one. And to keep my code clean, I would really like to separate my code in a .h and a .cpp. But the "export" keyword doesn't seem to be supported by VC++ ... Is there any way to avoid having only one big .h file with everything in it, when using template classes ? Thx for any advice.
Advertisement
As the export keyword is not supported by VC++ (Comeau is a compiler that does support it and the only one I know of that does) you've got to put all template code in header files and AFAIK there's no way around it.
Well, the usual solution to this is

template<typename T>class TemplatedClass{    //...};#include "TemplatedClass.inl"

"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
joanusdmentia, would you mind explaining that a little more ? Or point me to some article or anything ?

Thx for the replies anyway ^^
Two ways:
  • Write it as two separate files as you would normally but instead of #includeing the header at the start of the implementation, #include the implementation at the end of the header. You may also want to change the extension on the implementation from .cpp (or whatever you're using) to something like .tpp or .tpl to distinguish normal implmentation files, which need to be compiled as separate translation units, from template implementation files, which don't.

  • Explicit instantiation. Separate your files as normal and for each instantiated template type include an explicit template instantiation of the form template class /* class name */< /* template arguments */ >;

Note that the full template implementation is required to be visible at the point of explicit template instantiation, so such instantiations are typically placed at the end of the template implementation file.

Σnigma
Ok, thx Enigma. Your first solution seems to be the explenation of joanusdmentia's post ^^
Thx a lot.
Quote:Original post by paic
Ok, thx Enigma. Your first solution seems to be the explenation of joanusdmentia's post ^^
Thx a lot.


Yep [smile]
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
I think VC++ supports the .inl extension for inlined code (not soley) for the use of templated classes like you desire. Check out David Elberly's math library for Wild Magic, he does something very similar to what you want.
Steven ToveySPUify | Twitter
You can #include a file with any extension. You can have Visual Studio "interpret" any file extension as C++ code as well, though off the top of my head I can't recall where the setting is.

"export" itself isn't the magic bullet people assume it will be anyway. The templates still need to be available when instantiated; it does little to actually remove dependancies and more to hide them. I've found including .inl files to be the best method for writing templates w/out cluttering up the header.

This topic is closed to new replies.

Advertisement