Function Template

It’s been a while since I wrote about technicall stuff. This time I write about very basic feature of C++, function template. What I found intimidating at the first time learning C++ were memory management and template, so it’s normal if you say it’s not easy.
With function template you can create one function for different data type.

Normally to handle different data type we write different function or do overloading, lets create simple sizeOf function.

size_t sizeOf(int n)
{
 return sizeof(n);
}
size_t sizeOf(char n);
{
 return sizeof(n);
}
size_t sizeOf(const char* n);
{
 return sizeof(n);
}
int main(int argc, char **argv) {
 std::cout << sizeOf(40);
 std::cout << sizeOf('p');
 std::cout << sizeOf("petruk");
}

Look, they use same implementation therefore those code is not good.

we could rewrite the code above using function template.

template<typename T>
size_t sizeOf(T n)
{
 return sizeof(n);
}
int main(int argc, char **argv) {
 std::cout << sizeOf<int>(40);
 std::cout << sizeOf<char>('p');
 std::cout << sizeOf<const char*>("petruk");
}

Now we save a lot of lines of code with function template.

The structure of function template is really simple :
template<typename T>
[return type/it could be T] ([parameter / it could be T])
{
[implementation]
}

Any data type you put at T would replace all available T.

For example when you write :
sizeOf<int>(40)
virtually you write :
size_t sizeOf(int n)
{
 return sizeof(n);
}

However you must remember that template is not just simple text processing like preprocessor or macro.

3 thoughts on “Function Template

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s