The basics of programming in c++ for beginners

Templates C ++ functions

A little earlier, addressed the topic of function overloading in C++. You learned about the opportunity to write several functions with the same name, who perform the same tasks. The main difference between them – signature (parameter types and / or number of parameters). When the program starts, the compiler will choose, which of the overloaded functions apply(on the assumption that, that passed to the function)

The same templates allow functions to handle different data types, transmitted as parameters. But it is enough to write one common definition of a function. See how it looks.

function templates c ++, c ++ templates functions, template c++

By defining a function template we used C ++ reserved words: template and typename.   template suggests that, that will now define the template. And in angle brackets aftertypenamegiven a conditional name of the data type. Here, instead of name T, you can assign any (very desirable correct) name.

Next is determined by the function itself. Specify the type of the return value T. Options: the number of type T, and the number of type int. This function finds the number and percentage of the value in the program returns. And the number, and the percentage passed as parameters.   For example the first parameter we pass to an integer function (100).   During start-up, the compiler will note that currently T this is int and replace the template functions, all these Ton int. Thus, the compiler function has the form:

function templates c ++, c ++ templates functions, template c++

We do not see clearly, but everything works great. Whatever the number, we have not passed to this function as the first parameter, the compiler will create an instance of a function template for any call. Here, the first parameter can be any type of data.

As a result, the screen will see the result of this calculation:function templates c ++, c ++ templates functions, template c++

Speaking about the definition of a function template, i want to add, in angle brackets after the keyword template  possible to record several conventional names for data types. Why you might need to do this? Consider this example:

It is necessary to write a function, which will take two numbers, determine the maximum of them and return it in the program. Will be given, that function, we can pass the number of different types. Possible and the case, one number is an integer, and the second – material. See what happens:

The first and second parameters are defined function, as type parameters T. With function calls in strings 19-20 no problem, since the transfer parameters are of the same data type. Problems arise in the string 22  compile. And this is understandable. The compiler will get confused. He can not convert type int in double.

шаблоны функций c++, шаблоны функций с++, template c++

To work around this problem, it is necessary to write another template.

Heretypename T_1 indicates the type of parameter, that is passed in to the function first.  typename T_2  respectively indicates the type of the second parameter. This version of the definition of the function template will alert errors, resulting in the transmission of different types of parameters.

function templates c ++, c ++ templates functions, template c++

It is important to remember,if you are in angle brackets give you a few names for conventional types of function parameters (as in our example), all these type names must be mentioned in the function signature. Otherwise avoid errors when compiling. For example is the definition of a template

function templates c ++, c ++ templates functions, template c++

It leads to the following errors:

шаблоны функций c++, шаблоны функций с++, template c++

The template we defined them conditional types T_1 and T_2, and the signature of the prescribed type onlyT_1 .

In practice, you can meet the definition of a function template in such a way, where instead of the keyword typename the use of the word class.  For example:

function templates c ++, c ++ templates functions, template c++

it is equivalent to the definition of, which we considered

function templates c ++, c ++ templates functions, template c++

Previously, before the C ++ 98 standard, in templates always used the wordclass. Now better, when there was a keywordtypename , better to use it. Because it is more clearly says that, that the namesT_1 andT_2  They represent the type.

To summarize. The template function is designed to create a generalized description of the function. Such a function can take parameters of any type. The template allows the compiler to generate code for a specific function such as (or types) data, that was passed into it when you call.

Be sure to watch the video about templates:

18 thoughts on “Templates C ++ functions

  1. Sorry dilettante. Why use a template in the code for less?? We put the type of the return value of the function double or float In the parameters we also put either one or the other and forward. Everything is working.
    Something like this.

  2. Poorly and incomprehensibly explained.

    Especially in the last example, Where: “In the template, we have defined conditional types T_1 and T_2, and only the T_1 type was prescribed in the signature.”
    The signature is not visible at all here and so that, bl * db, for what – x-th understand!

  3. So in the last example, a mistake was made on purpose. So in the last example, a mistake was made on purpose
    So in the last example, a mistake was made on purpose, So in the last example, a mistake was made on purpose.

Leave a Reply to Guru Cancel reply

Your email address will not be published. Required fields are marked *