Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In <em><a href="http://amzn.com/0321334876" rel="noreferrer">Effective C++</a></em>, Scott Meyers notes in Item 2:</p> <blockquote> <p>Prefer <code>consts</code>, <code>enums</code>, and <code>inlines</code> to <code>#defines</code></p> </blockquote> <p>Specifically referring to the practice of writing macros instead of functions, Meyers says:</p> <blockquote> <p>Another common (mis)use of the <code>#define</code> directive is using it to implement macros that look like functions but don't incur the overhead of a function call.</p> <p>Macros like this have so many drawbacks, just thinking about them is painful.</p> <p>Fortunately, you don't have to put up with this nonsense. You can get all the efficiency of a macro plus all the predictable behavior and type safety of a regular function by using a template for an inline function.</p> <p>[Real functions] obey scope and access rules. For example, it makes perfect sense to talk about an inline function that is private to a class. In general, there's just no way to do that with a macro.</p> </blockquote> <p>To answer your questions specifically:</p> <ul> <li>Some programmers write macros instead of functions to avoid the perceived overhead of a function call -- a dubious practice that often has little measurable benefit. </li> <li>Programmers used to use the preprocessor to generate boilerplate code (and perhaps still do). In modern C++, use of the processor should be limited to <code>#include</code> and (maybe) <code>#ifdef</code>/<code>#ifndef</code> for conditional compilation. </li> </ul> <p>As Meyers notes in closing:</p> <blockquote> <p>It's not yet time to retire the preprocessor, but you should definitely give it long and frequent vacations.</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload