Skip to content
Related Articles

Related Articles

Improve Article

Difference between Preprocessor Directives and Function Templates in C++

  • Difficulty Level : Hard
  • Last Updated : 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;
}
Output:
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;
}
Output:
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:

S. No.Pre-processor directivesFunction Templates
1There is no type checkingThere is full type checking
2They are preprocessedThey are compiled
3They can work with any data typeThey are used with #define preprocessor directives
4They can cause an unexpected resultNo such unexpected results are obtained.
5They don’t ensure type safety in instanceThey do ensure type safety
6They have explicit full specializationThey don’t have explicit full specialization
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :