answersLogoWhite

0


Best Answer

Strictly speaking there is no need for function templates -- you could do it all by hand if you really wanted to. However, anything that aids in the reduction of duplicate source code is "a good thing" in my book. Code duplication increases code maintenance overheads, there's always the chance you'll forget to propagate changes consistently, and writing variations of a function that may or may not be used is simply a waste of time and effort.

To understand the need for function templates it's best to look at how they actually work. Let's consider the following two function declarations:

int GetMax(int a, int b){return(a>b?a:b);};

float GetMax(float a, float b){return(a>b?a:b);};

We can see that both functions have exactly the same body:

{return(a>b?a:b);};

These examples are fairly simple, but whether the function is complex or not, every time we need to pass a new data type to the GetMax() function we must declare a new definition of that function in order to handle that specific data type. Or we could write out every conceivable version of the GetMax() function in full, whether we intend to actually use it or not! That's certainly an option, but in a more complex function we may later decide to alter the implementation, in which case we must duplicate those changes across ever occurrence of the function. That's a lot of unnecessary work for just one function, and may introduce errors if we're not careful with our propagation of changes.

Thankfully, the compiler can do all this work for us. We need only define the function once, and once only, and the compiler will generate all the necessary code based upon the type of data we actually pass the function. This ensures we don't create copies of the function we will never use, and greatly reduces the maintenance overhead should we ever need to alter the implementation of the function.

We do this by using a function template.

template

T GetMax(T a, T b){return(a>b?a:b);};

Note that this is not a function, per se. It is a template for a function, whereby the variable T denotes a generic data type. Whenever we make a call to GetMax(), the compiler will automatically generate the necessary code for the actual function we need, replacing the generic data type T with the actual data type we pass into the function, such as int, or float.

int a=1, b=2;

int i=GetMax(i,j); // Generates the int variation of the function.

float x=0.3, y=1.2;

float f=GetMax(x,y); // Generates the float variation of the function.

Now that we have a function template, we are no longer restricted to ints and floats any more. We can now use GetMax() with any data type we wish, without having to write specific functions for those types. The compiler does it all for us, automatically.

DWORD p=0x000000FF, q=0x00000000;

DWORD d=GetMax(p,q); // Generates a new variation of the function.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

6y ago

The C++ standard template library (STL) provides us with many of the types we use every day, such as std::string. The most interesting classes are the generic containers, such as std::vector<T> which allows us to instantiate a variable length array for any given type T.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why do you use standard template library in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you enter a sentence as input in c plus plus?

Use the C++ getline() function from the standard library.


C plus plus reading and writing a file?

Use an input file stream (ifstream) to read from a file and an output file stream (ofstream) to write to a file. Both can be found in the &lt;fstream&gt; standard library header.


How do you write a C plus plus program that creates and manipulates a dynamic one dimensional array storing a string entered by user without using any C plus plus Standard functions?

In short you cannot, unless you provide a suitable alternative to the standard library yourself (in other words, reinvent the wheel). At the very least you will need to include the common runtime definitions (crtdefs.h) just to create a bare-bones program that does absolutely nothing, but you'll need to implement all the standard IO functions yourself. Even third-party replacements make extensive use of the standard library, so you're completely on your own here. The standard library exists so that you can draw upon a rich set of primitive but highly optimised, tried and tested functions which act as building blocks upon which you can create your own programs.


Why you use small letter in c or c plus plus programming language?

I assume you mean using lower case letters. By convention, C and C++ standard libraries use lower-case naming conventions. This makes it easy to identify functions and types that belong to the standard library. When defining your own types, a leading capital is preferred. All capitals typically denotes a macro definition.


What is a library function in C plus plus?

This are the predefined functions in c, which are already write.Examples : printf(),scanf().

Related questions

What is the c plus plus standard template library package you call to use the console for input and output?

The Standard Template Library does not provide routines or packages for I/O. You are probably thinking iostreams library, using cin and cout, which is part of the RunTime Library, but not part of the STL.


How do you enter a sentence as input in c plus plus?

Use the C++ getline() function from the standard library.


How do you find largest number in c plus plus?

Use the max() template function in the standard library. #include&lt;iostream&gt; int main() { int x=0, y=1; int z=std::max(x, y); // z=1 return(0); }


Why do you use ACADISO template in autocad?

This template sets your drawing space in conformity to standard A3 paper dimension.


What standard library you use in programming to read and write files?

You use fstream.Another answer: the standard C-run time library (also know as libc or C-rtl)


C plus plus reading and writing a file?

Use an input file stream (ifstream) to read from a file and an output file stream (ofstream) to write to a file. Both can be found in the &lt;fstream&gt; standard library header.


What can you use as a template for intaglio printing?

What else can you use as a template on intaglio printings


How do you write a C plus plus program that creates and manipulates a dynamic one dimensional array storing a string entered by user without using any C plus plus Standard functions?

In short you cannot, unless you provide a suitable alternative to the standard library yourself (in other words, reinvent the wheel). At the very least you will need to include the common runtime definitions (crtdefs.h) just to create a bare-bones program that does absolutely nothing, but you'll need to implement all the standard IO functions yourself. Even third-party replacements make extensive use of the standard library, so you're completely on your own here. The standard library exists so that you can draw upon a rich set of primitive but highly optimised, tried and tested functions which act as building blocks upon which you can create your own programs.


How to write C plus plus Program to take 2 distant values as argument and return the larger value on the screen?

Use the following template function: template&lt;class T&gt; T&amp; max(T&amp; x, T&amp; y){return(y&lt;x?x:y;}


How are Boost libraries useful in c plus plus?

Third-party C++ libraries in general are very useful. They provide us with types, algorithms and utilities that are not provided by the standard library alone and save us from continually having to "reinvent wheels". The standard library is specifically intended to be used by the vast majority of programmers across all fields of programming, whereas third-party libraries tend to be more specialised, providing support for graphics, sound and other proprietary hardware as well as types and algorithms that have more limited uses. However, although the Boost library is highly-specialised across a wide range of programming disciplines, many of its features have been incorporated into the standard library over the years. In some ways, the Boost library can be seen as a test-bed for future versions of the standard library as well as the language itself. However, due to its specialised nature, it will always remain separate from the standard library. Nevertheless, the Boost library is widely used and, if there's a feature you require that is not provided by the standard library, you will invariably find the Boost library either provides that feature directly or a third-party library makes use of the Boost library in order to provide that feature.


Can you use a free CSS template to create a Joomla template and host up this joomla template in my site for users to download?

Yes, I use free CSS template to create Joomla template. For getting css free template, visit globbersthemes.com, csschopper.com, Joomla 24.com


Why you use small letter in c or c plus plus programming language?

I assume you mean using lower case letters. By convention, C and C++ standard libraries use lower-case naming conventions. This makes it easy to identify functions and types that belong to the standard library. When defining your own types, a leading capital is preferred. All capitals typically denotes a macro definition.