Function templates in c++

Function templates in c++

A custom user-defined function template in the C++ programming language allows C++ users to create a function that can work with multiple individual custom user-generated data type objects. Instead of creating separate manual functions for data type methods like int, float, double, etc., C++ users can define or declare a generic function for multi-purpose use.

Function templates in c++

Syntax of function templates in C++ programming.

template <typename T>

T functionName(T p, T q)

{

// User-defined custom function template body

}

Here’s the function template syntax.

  • Here, template indicates to C++ programming that a custom user template is being defined.
  • In this, typename T, T is declared as a generic function data type method.
  • Here T represents the function template data type, such as int, float, double, char, etc.
  • When this user-defined template function is called, the C++ compiler manages the actual real-time template data type.

Example of a function template in C++ programming.

#include <iostream>

using namespace std;

template <typename T>

T max_value_is(T p, T q)

{

return (p > q) ? p : q;

}

int main()

{

cout << max_value_is(2, 7) << endl;

cout << max_value_is(7.9, 1.4) << endl;

return 0;

}

It’s output.

7

7.9

In this example, the same function template max_value_is() is used with both int and double custom data types in the function.

Function template with two different data types in C++.

In the C++ programming language, programmers can also use template parameter values ​​with multiple data types in a custom user-defined function template.

template <typename T, typename U>

void preview(T p, U q)

{

cout << p << ” ” << q << endl;

}

Calling a function template in C++.

preview(7, 3.4);

Here, T becomes an int data type value in this custom function template, and U becomes a double data type value.

Explicitly specifying a data type in C++.

C++ developers can explicitly indicate to the C++ compiler which data type values ​​to use in an existing user-defined custom function template.

cout << max_value_is<int>(4, 8);

Here, in this example, the function template T is explicitly indicated as the int data type.

Advantages of Function Templates in C++.

  • Code reusability – Create a function template once in your C++ program and test or use it with multiple custom data type values.
  • Reduces code duplication – You don’t need to create individual custom function declarations for each data type in each user-defined custom function template.
  • Type safety – The compiler checks the default data type method of a custom C++ function you declare during the compilation process.
  • Easy maintenance – Any modifications to the function templates you declare in a C++ program need to be made to only one template.

Function Templates vs. Normal Functions in the C++ Programming.

Normal c++ function behaviourFunction template concept
Normal c++ function usually works with a specific data type value or methodsFunction template in c++ can work with multiple data types at a same time with single custom function template method
Normal c++ function may require multiple overloaded program functions with multiple data type declarationFunction template in c++ need only one user define custom function template can handle many data types operation at a time
Normal c++ function required more code for program task or similar operationsIn c++ function template need less code and more reusable template functionality

Group Data Type Function Templates Example in C++.

#include <iostream>

#include <string>

using namespace std;

// User define custom Function Template

template <typename T>

void data_type_value(T datavalue) {

    cout << “Data Type Value Is – ” << datavalue << endl;

}

int main() {

    // Function template Integer data type value

    int p = 4;

    data_type_value(p);

    // Function template Short integer data type value

    short r = 30;

    data_type_value(r);

    // Function template Long integer data type value

    long s = 70000;

    data_type_value(s);

    // Function template Long long integer data type value

    long long t = 30000000000LL;

    data_type_value(t);

    // Function template Unsigned integer data type value

    unsigned int u = 80;

    data_type_value(u);

    // Function template Float data type value

    float v = 27.7f;

    data_type_value(v);

    // Function template Double data type value

    double w = 68.97;

    data_type_value(w);

    // Function template Long double data type value

    long double x = 40.9878956234L;

    data_type_value(x);

    // Function template Character data type value

    char y = ‘V’;

    data_type_value(y);

    // Function template Boolean data type value

    bool z = true;

    data_type_value(z);

    // Function template String data type value

    string c_name = “Vcanhelpsu Edtech”;

    data_type_value(c_name);

    return 0;

}

Conclusion of Function Templates in C++.

Function templates in the C++ programming language are a prime example of compile-time polymorphism. When a C++ user needs to use a function with a specific data type value, the C++ compiler displays the exact correct version information.

Function Templates In short, a function template in C++ is a user-defined custom generic function declaration method or definition that allows a single user-declared custom function value to operate on multiple data type method declarations.

Leave a Reply