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.
Template checks the data type
One parasmeter is integer and another in float
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.
There is no use of ++ or -- operator here
user 10 current_user 10 next_user 11
Calling the macro with ++ operator
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.
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 ";".
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.
Questions index C Questions C++ Questions Win32 MFC COM/DCOM DLL Questions
Compilers & Editors
Download Visual Studio Download XCode Download Visual Studio Code Android studio install sdk Eclipse installer Best C compilers IDEs
Development system setup
Windows media creation tool MSDN subscription Ubuntu virtualbox
New updated posts
Why learn C? Calculate weighted average