Difference between Preprocessor Directives and Function Templates in C++ - GeeksforGeeks

05 Jan, 2021

Preprocessor Directives are programs that process our source code before compilation. There are a number of steps involved between writing a program and executing a program in C / C++.
Below is the program to illustrate the functionality of Function Templates:
C++
 

 
 










// C++ program to illustrate 
// preprocessor directives 
#include <bits/stdc++.h> 
  
#define min(a, b) ((a < b) ? a : b) 
  
using namespace std; 
  
// Driver code 
int main() 
{ 
    int a = 2, b = 4; 
  
    // Find the minimum of a and b 
    cout << "Minimum of both is: "
         << min(a, b); 
  
    return 0; 
} 






Minimum of both is: 2

Function Templates are the generic function that can handle different data types without the need for any separate code.
Below is the program to illustrate the functionality of Function Templates:
C++
 

 
 










// C++ program to illustrate the use 
// of Function Templates 
#include <iostream> 
#include <stdio.h> 
using namespace std; 
  
// Function Template 
template <class T> 
T Min(T x, T y) 
{ 
    return (x < y) ? x : y; 
} 
  
// Driver Code 
int main() 
{ 
    int a = 4, b = 8; 
  
    // Find the minimum of a and b 
    cout << "Minimum of both is: " << min(a, b); 
  
    return 0; 
} 






Minimum of both is: 4

Function templates are used to make generic functions that can work with any data type. For example, the function template used for calculating the minimum of 2 values of any type can be defined as:
template <class T>
T minimum(T a, T b)
{
   return (a < b) ? a : b;
}
But, this task can also be performed using Pre-processor directives created using preprocessor directive #define. So, the minimum of the two numbers can be defined as as:
#define minimum(a, b) ((a < b) ? a : b)
The Pre-processor directives can also be achieved by using the below statements:
minimum(30, 35);
minimum(30.5, 40.5);
In C++, most of us prefer using templates over Pre-processor directives because:
In the case of Pre-processor directives, there is no type checking. But in the case of templates, full type checking is done by the compiler.
Pre-processor directives can call an unexpected result. Consider a macro that calculates the square of any number as:
#define sqr(x) (x*x)
Now if this macro is called using the following statement, x = sqr(4 + 4); then expected output is 64 but it generates 24 because any x in macro body replaced by 4 + 4 which leads to x = 4 + 4 * 4 + 4 = 24 but in the case of templates, such unexpected results are not obtained.
Below is the tabular difference between the two:

Templates


Articles


C++


C++ Programs


Difference Between


Programming Language

Writing code in comment? 
                                    Please use ide.geeksforgeeks.org, 
                                    generate link and share the link here.
                                
Time Complexity and Space Complexity
Docker - COPY Instruction
Time complexities of different data structures
Difference between Min Heap and Max Heap
SQL | Date functions
Vector in C++ STL
Arrays in C/C++
Initialize a vector in C++ (6 different ways)
Map in C++ Standard Template Library (STL)
Inheritance in C++