   Template member function compilation
  Basically, I would like to compile my templated class (every function of it!) without having to call each function to have each function compiled. I knew classes were only compiled when instantiated, but I did not know you had to call each function to compile each function!I have my template class set up in a .h and a .inl (inlcuding the .inl from the .h).I heard that you have to specialize to force compilation of every function in the class. Is this the only way?Thanks.EDIT: VS .NET 2005[Edited by - discman1028 on May 31, 2006 11:23:19 AM]
Quote:Original post by discman1028Basically, I would like to compile my templated class (every function of it!) without having to call each function to have each function compiled.I don't know the answer, but is this for you to build a library or dll? If not, then why would you want to do that? What would be the point of having a function compiled that you don't use?   ;  Good point I guess. Just to debug while writing the class, instead of down the road. Just the way I do things.ALSO: If you know the specialization trick that gets it to compile, let me know> I can't seem to get that to work. (Supposedly if you make a specialization of the class, it will force a compile.);  You can force the creation and compilation of specific instances of a class template with an explicit instantiation:template <typename T>class { ... };template class<int>;template class<float>;;  By taking a function pointer, you'll require that function to be compiled. That's about the least you can do to force compilation.;  I'd rather write a separate test program that not only forces the instantiation of your templates but exercises them meaningfully. You could compile and run it occasionally, without polluting your main program with dummy declarations and without adding to its compilation time.Another (even  better) approach is avoiding premature design and maintenance: if your templates work in their currently used specializations, leave them alone and add tests of new specializations only when you actually need them. You are trying to maintain code that could remain unnecessary or could be changed in the future, which is a very likely waste of time.Why do you have ".inl" files? They seem to be included from exactly one place, which defeats the purpose of #include.   ;Quote:Original post by discman1028I have my template class set up in a .h and a .inl (inlcuding the .inl from the .h).Is this atypical? When you inline, you do it, since inlining must be in the header. Since template definitions must be in the header, too, I give it the same .inl extension.I have <BLAH.h>:#ifndef BLAH_H_#define BLAH_H_template <typename T>class BLAH{ ...};#include "BLAH.inl"#endif BLAH_H_[Edited by - discman1028 on June 1, 2006 9:28:48 AM];  New question:Realizing this was called "explicit instantiation" helped me greatly. Now, I'd like to compile a templated interface (a templated class with a pure virtual function). Is there a way to do this?Is it something like:template class CTestDerived : public IInterface<bool>;?   ;  Yeah, but there's no need for the template keyword because CTestDerived's definition is not based on template parameters. Just write:class CTestDerived : public IInterface<bool>{// Everything you want in your class};;  You should probably look into proper unit testing. I use the Tut Unit Test Framework, as it's very simple and lightweight; however, there are others so look around.In general, you should have about as much test code as normal code (or so the rule of thumb goes).   