Home > OOP344 > Function Templates Example

Function Templates Example

It is common to see functions that do almost the same thing but for different data types. We don’t need to write that code many times and just change the data types. The correct solution is to use function templates. I have written a swap function to demonstrate the use of function templates.

template <template T>
void swap(T a, T b){
T temp;
temp = a;
a = b;
b = temp;
}

we the function template above, we can pass it an int or float and the compiler will automatically create separate functions as needed.

Categories: OOP344
  1. No comments yet.
  1. No trackbacks yet.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: