{"id":263,"date":"2016-03-19T13:10:57","date_gmt":"2016-03-19T10:10:57","guid":{"rendered":"https:\/\/sobbayi.com\/?p=263"},"modified":"2016-03-19T13:10:57","modified_gmt":"2016-03-19T10:10:57","slug":"introduction-to-cpp-templates","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/introduction-to-cpp-templates\/","title":{"rendered":"Demystifying C++ Templates for Beginner Programmers"},"content":{"rendered":"

Today we take a break. After the sixth part in our tutorial series on the C++ Standard Template Library<\/a>, we briefly talk about what C++ templates are. For the past few months, we have been discussing the STL. At the heart of the STL is the template. In spite of that, we have not really made a beginner introduction to C++ templates.<\/p>\n

Just in case you are already familiar with templates in C++, you can skip this tutorial and go straight to and start the tutorial on the Standard Template Library series<\/a>. <\/p>\n

A Case for C++ Templates<\/h2>\n

In computing, a template can be described as a preset format for a function, document or file, used so that the format does not have to be recreated each time it is used. In the C++ language, this definition holds true. To give a better illustration we will be using some code examples, therefore, we do make assumptions that you are already familiar with how to write functions in C++.<\/p>\n

If you are not familiar with C++ functions then may need to take the modern C++ tutorial series we will be publishing here on Sobbayi Softworks. For the longest time, one of the strengths of C++ has been its ability to allow the programmer to program in templates and the containers found in the C++ Standard Template Library are a good example of the power of C++ templates. The code is very stable fast and efficient and comes highly recommended for use wherever it can be used. We will not be talking too much about STL here as there is an entire series on that.<\/p>\n

Just as a point to note. There are other third party libraries out there such as Boost<\/a> that make great use of templates and I would also advise you to check them out here<\/a>.<\/p>\n

Assuming we have a function that does something trivial like simple arithmetic, The following code would satisfy our needs perfectly:<\/p>\n