Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat are preprocessor macros good for?
    text
    copied!<p>After reading another question about the use of macros, I wondered: What <em>are</em> they good for?</p> <p>One thing I don't see replaced by any other language construct very soon is in diminishing the number of related words you need to type in the following:</p> <pre><code>void log_type( const bool value ) { std::cout &lt;&lt; "bool: " &lt;&lt; value; } void log_type( const int value ) { std::cout &lt;&lt; "int: " &lt;&lt; value; } ... void log_type( const char value ) { std::cout &lt;&lt; "char: " &lt;&lt; value; } void log_type( const double value ) { std::cout &lt;&lt; "int: " &lt;&lt; value; } void log_type( const float value ) { std::cout &lt;&lt; "float: " &lt;&lt; value; } </code></pre> <p>as opposed to</p> <pre><code>#define LOGFN( T ) void log_type( const T value ) { std::cout &lt;&lt; #T ## ": " &lt;&lt; value; } LOGFN( int ) LOGFN( bool ) ... LOGFN( char ) LOGFN( double ) LOGFN( float ) </code></pre> <p>Any other 'irreplaceables'?</p> <p>EDIT: trying to summarize the reasons-why encountered in the answers; since that's what I was interested in. Mainly because I have a feeling that most of them are due to us still programming in raw text files in, still, poorly supporting environments.</p> <ul> <li>flexibility of code-to-be compiled (e.g. <code>#ifdef DEBUG</code>, platform issues) (SadSido, Catalin, Goz)</li> <li>debug purposes (e.g. <code>__LINE__, __TIME__</code>); I also put 'stringifying' under this reason (SadSido, Jla3ep, Jason S)</li> <li>replacing e.g. PHP's <code>require</code> vs. <code>include</code> feature (<code>#pragma once</code>) (SadSido, Catalin)</li> <li>readability enhancement by replacing complicated code (e.g. <code>MESSAGEMAP</code>, <code>BOOST_FOREACH</code>) (SadSido, fnieto)</li> <li>DRY principle (Jason S)</li> <li>an inline replacement (Matthias Wandel, Robert S. Barnes)</li> <li>stringifying (Jason S)</li> </ul>
 

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