Templates and Exception Handling in C++
TEMPLATES
• Template is a concept which enables us to define generic classes and functions and thus provides support for generic programming
• Generic types are used as parameters in programs so that they work for a variety of suitable data types
• For instance, in a class template, when an object of a specific type is defined for actual use, the template definition for that class is substituted with the required data type
FUNCTION TEMPLATE
• C++ allows the use of function template, which is a generic type of function
• The type is determined at compile time
• Function template cannot qualify unless it has at least one argument of generic type
• In function overloading, there are as many function declarations and function definitions as the number of different function signatures we need
• Advantages of function template over function overloading
- No code repetition
- Less chance of errors and bugs
- Object code gets reduced and hence the execution speed is enhanced
General Syntax:
template
• ‘template’ and ‘class’ are keywords
• ‘T’ is a generic type
• The return type can be a generic type or any basic data type