Exporting template class
-
Hi, I'm unable to export a template class from an MFC based dll ! any ideas of how I can do it ? Ciao - Nilesh
Template classes are generated by their template arguments, they are in fact a little class factory. So I reckon you'll have trouble. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002 -
Hi, I'm unable to export a template class from an MFC based dll ! any ideas of how I can do it ? Ciao - Nilesh
You can export a template class as long as you force the compiler to create an instance of the class that you are trying to export. That is because a template is a compile time feature. You can force the compiler to generate the code for a particular type of object by simply declaring a global variable of that particular type. But for each type you declare the size of your DLL will grow. There is no way to export a template from a DLL, an retain the power that a template provides. Would it simply be possible for you to distribute the header file with the template class in it instead?
Build a man a fire, and he will be warm for a day
Light a man on fire, and he will be warm for the rest of his life! -
Hi, I'm unable to export a template class from an MFC based dll ! any ideas of how I can do it ? Ciao - Nilesh
I once did what you are trying to do (for export to a non-C++ project), but as Paul mentioned, the benefits of templates are lost. One way is to create a non-templatised wrapper class, and wrap every single method so that it calls the template version. Make sure you inline everything to avoid extra call overhead. If you forget to wrap any methods, the compiler will not generate them at all. Of course, you will have to declare one specific type to be used with the exported class, hence the class will no longer be generic. You can create separate wrapper classes for different types, though your DLL will get rather large. Cheers