Macro vs Template

Macro are used in C and C++ for replacement of numbers, small inline functions etc.

Template is only available in C++ language. It is used to write small macro like functions etc.

Data type check in macro

Macro cannot check the data type of arguments. This below code compiles fine.

#define swap(x, y)\

int main ()
{
  int i = 10;
  float j = 12.2;
  swap(i, j);
}

Template checks the data type

#include <iostream>
template <typename T> 
 T swap(T &a, T &b)
{
  T c;
  c = b;
  b = a;
  a = c;
}
int main ()
{
  int i = 10;
  float j = 12.2;
  swap(i, j);
}

One parasmeter is integer and another in float

g++ macro_template.cpp 
macro_template.cpp:13:3: error: no matching function for call to 'swap'
  swap(i, j);
  ^~~~
macro_template.cpp:2:4: note: candidate template ignored: deduced conflicting types for
      parameter 'T' ('int' vs. 'float')
 T swap(T &a, T &b)
   ^
1 error generated.

Operators used in macro

If parameters of any macro are given with operators ++ and -- with it and the macro used this argument more than one times then the increment or decrement of the original variable will that much times.

#include <stdio.h>

#define set_userid(x)\
current_user = x; \
next_user = x + 1;
#if 0
int main ()
{
int user = 10;
int current_user, next_user;

set_userid(user);

printf ("user %d\n", user);
printf ("current_user %d\n", current_user);
printf ("next_user %d\n", next_user);
return 0;
}

There is no use of ++ or -- operator here

user 10
current_user 10
next_user 11

Calling the macro with ++ operator

#include <stdio.h>

#define set_userid(x)\
current_user = x; \
next_user = x + 1;

int main ()
{
int user = 10;
int current_user, next_user;

set_userid(user++);
/* preprossing code * /* current_user = x++; */
/* next_user = x++ + 1; */

printf ("user %d\n", user);
printf ("current_user %d\n", current_user);
printf ("next_user %d\n", next_user);
return 0;
}

See the value of user incremented twice.

user 12
current_user 10
next_user 12

Compilation error in macro

Macros are expanded by the preprocessor and then compilation takes place. Compiler will refer error messages in expanded macro or the line where macro has been called.

We have a missing ";" in line 2 in the macro.

Line:1:#define set_userid(x)\
Line:2:current_user = x \
Line:3:next_user = x + 1; 
Line:4:int main (int argc, char *argv[])
Line:5:{
Line:6:  int user = 10;
Line:7:  set_userid(user);
Line:7:}

Compiler is not giving the error on line 2 where we have missing ";". It is giving the error where the macro is called.

main.c(7):error C2146: syntax error : missing ';' before identifier 'next_user'

Here is an error inside template at c = b statement with no ";".

#include <iostream>
template <typename T> 
 T swap(T &a, T &b)
{
  T c;
  c = b
  b = a;
  a = c;
}
int main ()
{
  int i = 10;
  int j = 12;
  swap(i, j);
}

Compilation error points to the line of the template not like the case of macro.

$ g++ template.cpp 
template.cpp:6:8: error: expected ';' after expression
  c = b
       ^
       ;
1 error generated.

About our authors: Team EQA

You have viewed 1 page out of 62. Your C++ learning is 0.00% complete. Login to check your learning progress.

#