Templates C ++ functions

Templates C ++ functions
5 (100%) 3 votes





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

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 after typename given 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 T on 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 of. The case is, 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.

Here typename 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 only T_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 word class. Now better, when there was a keyword typename , better to use it. Because it is more clearly says that, that the names T_1 and T_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.

Must see video. About templates with a 14-minute:

Newsletter of programming:

15 thoughts on “Templates C ++ functions

  1. А можно сделать переменную универсальной?То есть она может быть типом double,а так же и int.

Leave a Reply

Place code in tags: <pre class="lang:c++ decode:true ">YOUR CODE</pre>