C++ Macros Functionality

What is the main thing that C++ macros can do that cannot be done with functions? Give a realistically useful example of something of this kind.
Macros have the distinct advantage of being more efficient than functions, because their corresponding code is inserted directly into your source code at the point where the macro is called. There is no overhead involved in using a macro like there is in placing a call to a function.
Is this right?

Showing Answers 1 - 2 of 2 Answers

EigenCritic@gmail.com

  • Apr 6th, 2016
 

People usually say that using macros in C++ should be avoided. But there are exceptions, especially regarding meta programming, debugging, profiling and for cross-platform code switches.
You can use inline functions if you just want no overhead.
Be careful writing macros, it can be really dangerous to your code, hard to read, hard to debug.
Example:
If you go to from standard library Visual Studio 2015 you will find the following code:
The first part is a code switch for using or not using exceptions,
The second one is meta programming, adding a bunch of operators with one line in any class

Code
  1. #if _HAS_EXCEPTIONS

  2.  #define _TRY_BEGIN     try {

  3.  #define _CATCH(x)      } catch (x) {

  4.  #define _CATCH_ALL     } catch (...) {

  5.  #define _CATCH_END     }

  6. ...

  7. #else /* _HAS_EXCEPTIONS */

  8.  #define _TRY_BEGIN     { if (1) {

  9.  #define _CATCH(x)      } else if (0) {

  10.  #define _CATCH_ALL     } else if (0) {

  11.  #define _CATCH_END     } }

  12. ...

  13.                                 if (_Buy(_Right.size()))

  14.                                         _TRY_BEGIN

  15.                                         this->_Mylast() =

  16.                                                 _Ucopy(_Right._Myfirst(), _Right._Mylast(),

  17.                                                 this->_Myfirst());

  18.                                         _CATCH_ALL

  19.                                         _Tidy();

  20.                                         _RERAISE;

  21.                                         _CATCH_END

  22.                                 }

  23. ...

  24. // And some of my favorite code in <vector>, A dance of dash and slashes

  25.  #define _BITMASK_OPS(Ty)

  26. inline Ty& operator&=(Ty& _Left, Ty _Right)

  27.         {       /* return _Left &= _Right */

  28.         _Left = (Ty)((int)_Left & (int)_Right); return (_Left);

  29.         }

  30.  

  31. inline Ty& operator|=(Ty& _Left, Ty _Right)

  32.         {       /* return _Left |= _Right */

  33.         _Left = (Ty)((int)_Left | (int)_Right); return (_Left);

  34.         }

  35.  

  36. ...

  Was this answer useful?  Yes

Joe Fitzgerald

  • Jun 5th, 2017
 

Use macros for an "include guard" only to avoid duplicate definition of types. Macros should be avoided in most other cases in C++ since they are not type safe, fail to respect scoping rules, and they can produce confusing error messages when they fail to compile.

Code
  1. #ifndef HEADER_FILE_H

  2. #define HEADER_FILE_H

  3.  

  4. struct foo {

  5.     int member;

  6. };

  7.  

  8. #endif /* HEADER_FILE_H */

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions