Reputation: 33
Suppose I wrote a class library in my project, and I would not use all of the classes/ functions available. Would it make any sense to write a non-template function as a template like this:
template<>
void doStuff(int argument)
{
// Stuff done here
return;
}
I presume that template rules might apply here, so if I didn't want to doStuff at all, and do not call that function, it would not have to be instanciated at all, would it? Can I also use this technique for classes, or is there something I didn't catch about templates?
So does this save space?
Upvotes: 0
Views: 61
Reputation: 217245
Your try is a template specialization.
To have function template without template parameter, you may use default template parameter:
template<typename = void>
void doStuff(int argument)
{
// Stuff done here
}
but you may instead inline it:
inline void doStuff(int argument)
{
// Stuff done here
}
but in fact less the linker/compiler do their job and only if it becomes a problem investigate how to solve this issue.
Upvotes: 1