Jump to content
  • Advertisement
Sign in to follow this  
discman1028

Template member function compilation

This topic is 4515 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by discman1028
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 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?

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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>;

Share this post


Link to post
Share on other sites
By taking a function pointer, you'll require that function to be compiled. That's about the least you can do to force compilation.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by discman1028
I 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]

Share this post


Link to post
Share on other sites
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>;

?

Share this post


Link to post
Share on other sites
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
};

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!