Skip to main content
gamedev.net
🔒 Locked

Template member function compilation

Started by discman1028 May 31, 2006 at 11:04 AM 10 replies 1.1k views
Original Post
discman1028
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 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]
--== discman1028 ==--
Endar
Endar
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?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
discman1028
discman1028
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.)
--== discman1028 ==--
Sharlin
Sharlin
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>;
Troll
Troll
By taking a function pointer, you'll require that function to be compiled. That's about the least you can do to force compilation.
LorenzoGatti
LorenzoGatti
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.
Omae Wa Mou Shindeiru
discman1028
discman1028
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 :

#ifndef BLAH_H_
#define BLAH_H_

template
class BLAH
{
...
};

#include "BLAH.inl"

#endif BLAH_H_

[Edited by - discman1028 on June 1, 2006 9:28:48 AM]
--== discman1028 ==--
discman1028
discman1028
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;

?
--== discman1028 ==--
Troll
Troll
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
{
// Everything you want in your class
};
omgomghilol
omgomghilol
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).
discman1028
discman1028
Quote:
Original post by omgomghilol
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).


I'll look into Tut -- thanks.
--== discman1028 ==--
discman1028
discman1028
Quote:
Original post by Troll
Just write:

class CTestDerived : public IInterface
{
// Everything you want in your class
};


I'm not sure that's an explicit instantiation. That's how I derive a class from the interface, but I think explicit instantiation syntax might be different. Can anyone confirm this?
--== discman1028 ==--

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.