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.
Chat with our AI personalities
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.
Use the C++ getline() function from the standard library.
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 <fstream> standard library header.
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.
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.
This are the predefined functions in c, which are already write.Examples : printf(),scanf().