ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Object Oriented Programming
    CC-211
    Progress0 / 24 topics
    Topics
    1. Object-Oriented Design: History and Advantages2. Object-Oriented Programming: Terminology and Features3. Classes and Objects4. Data Encapsulation5. Constructors and Destructors6. Access Modifiers7. Const vs Non-Const Functions8. Static Data Members and Functions9. Function Overloading10. Operator Overloading11. Identification of Classes and Their Relationships12. Composition13. Aggregation14. Inheritance15. Multiple Inheritances16. Polymorphism17. Abstract Classes18. Interfaces19. Generic Programming Concepts20. Function Templates21. Class Templates22. Standard Template Library23. Object Streams: Data and Object Serialization24. Exception Handling
    CC-211›Function Templates
    Object Oriented ProgrammingTopic 20 of 24

    Function Templates

    8 minread
    1,372words
    Intermediatelevel

    Function Templates in C++

    A function template in C++ allows you to define a function that works with any data type. Instead of writing multiple versions of a function for different types, a function template enables you to write a single generic function that can handle multiple types of arguments. The exact type to be used is determined at compile-time, when the function is invoked.

    Function templates provide the flexibility to create generic, reusable code that operates with a variety of types, enhancing code maintainability and readability.

    Syntax of Function Templates

    To define a function template, you use the template keyword followed by a template parameter list enclosed in angle brackets (< >). The template parameter acts as a placeholder for the type that will be specified when the function is called.

    template <typename T>
    T function_name(T arg1, T arg2) {
        // function body
    }
    

    Here:

    • template <typename T>: This defines a template parameter T, which represents a type that will be substituted when the function is called.
    • T function_name(T arg1, T arg2): The function function_name takes two arguments of type T and returns a result of type T.

    Example of a Function Template

    #include <iostream>
    using namespace std;
    
    // Function template to add two values
    template <typename T>
    T add(T a, T b) {
        return a + b;
    }
    
    int main() {
        cout << add(5, 3) << endl;          // T is int
        cout << add(3.5, 2.7) << endl;      // T is double
        cout << add(1.2f, 2.3f) << endl;    // T is float
        return 0;
    }
    

    Output:

    8
    6.2
    3.5
    

    Explanation:

    • In the function add, the template parameter T allows the function to work with any type, such as int, double, or float. When the function is called in main(), the compiler automatically deduces the type of T based on the type of the arguments passed.
    • This avoids the need for writing separate add functions for int, double, float, etc.

    Template Function with Multiple Template Parameters

    You can define a function template that accepts multiple parameters of different types. Each parameter can have its own template type.

    template <typename T, typename U>
    auto multiply(T a, U b) -> decltype(a * b) {
        return a * b;
    }
    
    int main() {
        cout << multiply(3, 4.5) << endl;       // T is int, U is double
        cout << multiply(2.5, 4.5) << endl;     // T and U are both double
        return 0;
    }
    

    Output:

    13.5
    11.25
    

    Explanation:

    • In this example, the multiply function takes two parameters a and b of types T and U respectively. The return type is automatically deduced using decltype(a * b), which evaluates the type of the product of a and b.
    • This allows the function to handle different combinations of types and return a result of the appropriate type.

    Function Template with Default Arguments

    You can also provide default values for the template parameters. If a type is not explicitly specified when the function is called, the default type will be used.

    template <typename T = int>  // Default type is int
    T square(T x) {
        return x * x;
    }
    
    int main() {
        cout << square(4) << endl;      // Uses default type 'int'
        cout << square(3.5) << endl;    // T is double
        return 0;
    }
    

    Output:

    16
    12.25
    

    Explanation:

    • In this example, the template parameter T has a default type of int. If no type is specified when calling square, int will be used. However, if a different type (e.g., double) is provided, the template will adapt accordingly.

    Template Function Overloading

    You can overload a function template by defining multiple versions of a function template with different numbers or types of parameters.

    template <typename T>
    void print(T t) {
        cout << "Printing value: " << t << endl;
    }
    
    template <typename T, typename U>
    void print(T t, U u) {
        cout << "Printing values: " << t << " and " << u << endl;
    }
    
    int main() {
        print(5);                  // Single argument (int)
        print(3.14, "Hello");      // Two arguments (double and string)
        return 0;
    }
    

    Output:

    Printing value: 5
    Printing values: 3.14 and Hello
    

    Explanation:

    • Here, two versions of the print function template are defined: one for printing a single argument and another for printing two arguments.
    • The compiler will choose the correct function based on the number and types of the arguments provided when calling the function.

    Template Argument Deduction

    In C++, when calling a function template, the compiler tries to deduce the type of the template parameter based on the arguments passed. This process is known as template argument deduction.

    template <typename T>
    void print(T value) {
        cout << "Value: " << value << endl;
    }
    
    int main() {
        int x = 10;
        print(x);   // T is deduced as int
        print(3.14); // T is deduced as double
        return 0;
    }
    

    Output:

    Value: 10
    Value: 3.14
    

    Explanation:

    • The compiler automatically deduces that T is int when x (an integer) is passed to print, and T is double when the literal 3.14 is passed.

    SFINAE (Substitution Failure Is Not An Error)

    SFINAE is a feature of C++ that allows you to enable or disable function templates based on certain conditions. It is used to create type traits or conditional template instantiations.

    You can use SFINAE to enable a function template only if a specific condition is met, such as if a type has certain properties (e.g., if it's an integer type or a floating-point type).

    #include <iostream>
    #include <type_traits>
    using namespace std;
    
    template <typename T>
    typename std::enable_if<std::is_integral<T>::value>::type print(T value) {
        cout << "Integral value: " << value << endl;
    }
    
    template <typename T>
    typename std::enable_if<std::is_floating_point<T>::value>::type print(T value) {
        cout << "Floating-point value: " << value << endl;
    }
    
    int main() {
        print(10);        // Integral type
        print(3.14);      // Floating-point type
        return 0;
    }
    

    Output:

    Integral value: 10
    Floating-point value: 3.14
    

    Explanation:

    • In this example, we use std::enable_if in conjunction with std::is_integral and std::is_floating_point to selectively enable function templates based on the type of the argument.
    • The first function template is enabled only for integral types (int, long, etc.), and the second is enabled for floating-point types (float, double, etc.).

    Summary of Key Concepts

    1. Function Templates: A template for creating functions that can work with any data type. The actual type is specified when the function is called.

    2. Template Parameters: Template parameters can be of any type (e.g., T) and can be specialized for different types or used with non-type parameters like constants or integers.

    3. Multiple Template Parameters: Functions can have more than one template parameter, enabling functions to operate with multiple data types.

    4. Default Template Arguments: You can provide default values for template parameters, so they don't have to be specified every time.

    5. Overloading Templates: Functions can be overloaded based on the number or types of template parameters.

    6. Template Argument Deduction: The compiler can automatically deduce the template argument based on the function call, making template functions very flexible.

    7. SFINAE: Used to enable or disable certain function templates based on type properties (e.g., integral, floating-point).

    Advantages:

    • Code Reusability: One function can work with multiple types, reducing redundancy.
    • Type Safety: Template code ensures type safety at compile-time.
    • Maintainability: Template-based code is easier to maintain and extend because new types can be added without changing the core algorithm.

    Disadvantages:

    • Compilation Time: Template code can increase compilation times due to instantiation for each type.
    • Error Messages: Template errors can be complex and harder to debug due to the nature of template metaprogramming.

    Function templates are one of the most powerful features in C++ that enable the writing of flexible, type-independent functions.

    Previous topic 19
    Generic Programming Concepts
    Next topic 21
    Class Templates

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time8 min
      Word count1,372
      Code examples0
      DifficultyIntermediate