Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this:</p> <pre><code>#ifdef SOMETHING #define foo(x) #else #define foo(x) MyFunction(x) #endif </code></pre> <p>If your function has several arguments, then:</p> <pre><code>#ifdef SOMETHING #define foo(x,y,z) #else #define foo(x,y,z) MyFunction(x,y,z) #endif </code></pre> <p>If your function has a variable number of arguments, then your compiler may support so-called "variadic macros", like this:</p> <pre><code>#ifdef SOMETHING #define foo(...) #else #define foo(...) MyFunction(__VA_ARGS__) #endif </code></pre> <p>The reason which I've seen this kind of thing used in practice is to get rid of logging functions from a release build. However, see also <a href="https://stackoverflow.com/questions/420343/separate-debug-and-release-builds">Separate &#39;debug&#39; and &#39;release&#39; builds?</a> in which people question whether you <em>should</em> even <em>have</em> different builds.</p> <hr> <p>Alternatively, instead of redefining the function call as nothing, Jonathan's comment to this answer suggested doing something like the following:</p> <pre><code>#ifdef SOMETHING #define foo(...) do { if (false) MyFunction(__VA_ARGS__) } while (0) #else #define foo(...) do { if (true) MyFunction(__VA_ARGS__) } while (0) #endif </code></pre> <p>The reasoning for doing this is so that the function call is always compiled (so it won't be left with gratuitous errors like references to deleted variables), but only called when needed: see Kernighan &amp; Pike <a href="http://rads.stackoverflow.com/amzn/click/020161586X" rel="nofollow noreferrer">The Practice of Programming</a> and also the <a href="http://software.gsfc.nasa.gov/assetsbytype.cfm?TypeAsset=Standard" rel="nofollow noreferrer">Goddard Space Flight Center programming standards</a>.</p> <p>From a debug.h file (originating from 1990, and therefore not using <code>__VA_ARGS__</code>):</p> <pre><code>/* ** Usage: TRACE((level, fmt, ...)) ** "level" is the debugging level which must be operational for the output ** to appear. "fmt" is a printf format string. "..." is whatever extra ** arguments fmt requires (possibly nothing). ** The non-debug macro means that the code is validated but never called. ** -- See chapter 8 of 'The Practice of Programming', by Kernighan and Pike. */ #ifdef DEBUG #define TRACE(x) db_print x #else #define TRACE(x) do { if (0) db_print x; } while (0) #endif /* DEBUG */ </code></pre> <p>With C99, there's no longer a need for the double parentheses trick. New code should not use it unless C89 compatibility is an issue.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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