Skip to main content
GameDev.net gamedev.net
🔒 Locked

Template class and inline definitions

Started by Akusei Nov 26, 2011 at 6:54 PM 3 replies 950+ views
Original Post
Akusei
Akusei
I know that if I create a template class, I need to include the member function definitions inside the .h file as well but I was wondering if I need to define ALL member functions in the .h file or just the ones that use the typename defined by the template keyword?

Is the below example ok or do I need to define all functions in the header file even if "T" is not touched by 3 of the member functions/constructors/destructors?
NOTE: Yes, I know the below code is not functional, it does not compile and there are errors in it (e.g. T value[];), it is an example only and is used to illustrate my question

[source lang="cpp"]
template
class TestClass
{
TestClass();
~TestClass();

void DoSomethingThatDoesNotTouchT();

T GetValue(int x)
{
return value[x];
}

void SetValue(int x, T v)
{
value[x] = v;
}

private:
T value[];
};
[/source]
-akusei
SiCrane
SiCrane
Without explicit template instantiation, or the rare compiler that supports the export keyword, all used template function definitions must be available at point of use. If your function doesn't depend on the template parameter in anyway, make it a non-member non-template function and you can define it like any non-template function.
Akusei
Akusei

Without explicit template instantiation, or the rare compiler that supports the export keyword, all used template function definitions must be available at point of use. If your function doesn't depend on the template parameter in anyway, make it a non-member non-template function and you can define it like any non-template function.


By "non-member" function do you mean a free function or a member function of a different non-template class? It doesn't make sense for the member functions in my class that don't make use of the template parameter to not be in that class. Maybe I'm just misunderstanding what you mean?
-akusei
SiCrane
SiCrane
It doesn't matter if it actually uses the template parameter or not, if it's a template function, it needs to have its definition available at point of use (or explicit instantiation or the export keyword). After all, the compiler can't tell if it uses the template parameter or not if it can't see that definition. If it doesn't use the template parameter, then you can define it as a non-template free function or a non-template member of a common non-template base class.
Akusei
Akusei

It doesn't matter if it actually uses the template parameter or not, if it's a template function, it needs to have its definition available at point of use (or explicit instantiation or the export keyword). After all, the compiler can't tell if it uses the template parameter or not if it can't see that definition. If it doesn't use the template parameter, then you can define it as a non-template free function or a non-template member of a common non-template base class.


Got it, thanks
-akusei

Topic Locked

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

Sign in to reply to this topic.