⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 self-referential-macros.html

📁 gcc手册
💻 HTML
字号:
<html lang="en">

<head>

<title>The C Preprocessor</title>

<meta http-equiv="Content-Type" content="text/html">

<meta name="description" content="The C Preprocessor">

<meta name="generator" content="makeinfo 4.3">

<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">

<!--

Copyright &copy; 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996,

1997, 1998, 1999, 2000, 2001, 2002, 2003

Free Software Foundation, Inc.



   <p>Permission is granted to copy, distribute and/or modify this document

under the terms of the GNU Free Documentation License, Version 1.1 or

any later version published by the Free Software Foundation.  A copy of

the license is included in the

section entitled "GNU Free Documentation License".



   <p>This manual contains no Invariant Sections.  The Front-Cover Texts are

(a) (see below), and the Back-Cover Texts are (b) (see below).



   <p>(a) The FSF's Front-Cover Text is:



   <p>A GNU Manual



   <p>(b) The FSF's Back-Cover Text is:



   <p>You have freedom to copy and modify this GNU Manual, like GNU

     software.  Copies published by the Free Software Foundation raise

     funds for GNU development. 

-->

</head>

<body>

<div class="node">

<p>

Node:<a name="Self-Referential%20Macros">Self-Referential Macros</a>,

Next:<a rel="next" accesskey="n" href="Argument-Prescan.html#Argument%20Prescan">Argument Prescan</a>,

Previous:<a rel="previous" accesskey="p" href="Duplication-of-Side-Effects.html#Duplication%20of%20Side%20Effects">Duplication of Side Effects</a>,

Up:<a rel="up" accesskey="u" href="Macro-Pitfalls.html#Macro%20Pitfalls">Macro Pitfalls</a>

<hr><br>

</div>



<h4 class="subsection">Self-Referential Macros</h4>



   <p>A <dfn>self-referential</dfn> macro is one whose name appears in its

definition.  Recall that all macro definitions are rescanned for more

macros to replace.  If the self-reference were considered a use of the

macro, it would produce an infinitely large expansion.  To prevent this,

the self-reference is not considered a macro call.  It is passed into

the preprocessor output unchanged.  Let's consider an example:



<pre class="example">     #define foo (4 + foo)

     </pre>



<p>where <code>foo</code> is also a variable in your program.



   <p>Following the ordinary rules, each reference to <code>foo</code> will expand

into <code>(4 + foo)</code>; then this will be rescanned and will expand into

<code>(4 + (4 + foo))</code>; and so on until the computer runs out of memory.



   <p>The self-reference rule cuts this process short after one step, at

<code>(4 + foo)</code>.  Therefore, this macro definition has the possibly

useful effect of causing the program to add 4 to the value of <code>foo</code>

wherever <code>foo</code> is referred to.



   <p>In most cases, it is a bad idea to take advantage of this feature.  A

person reading the program who sees that <code>foo</code> is a variable will

not expect that it is a macro as well.  The reader will come across the

identifier <code>foo</code> in the program and think its value should be that

of the variable <code>foo</code>, whereas in fact the value is four greater.



   <p>One common, useful use of self-reference is to create a macro which

expands to itself.  If you write



<pre class="example">     #define EPERM EPERM

     </pre>



<p>then the macro <code>EPERM</code> expands to <code>EPERM</code>.  Effectively, it is

left alone by the preprocessor whenever it's used in running text.  You

can tell that it's a macro with <code>#ifdef</code>.  You might do this if you

want to define numeric constants with an <code>enum</code>, but have

<code>#ifdef</code> be true for each constant.



   <p>If a macro <code>x</code> expands to use a macro <code>y</code>, and the expansion of

<code>y</code> refers to the macro <code>x</code>, that is an <dfn>indirect

self-reference</dfn> of <code>x</code>.  <code>x</code> is not expanded in this case

either.  Thus, if we have



<pre class="example">     #define x (4 + y)

     #define y (2 * x)

     </pre>



<p>then <code>x</code> and <code>y</code> expand as follows:



<pre class="example">     x    ==&gt; (4 + y)

          ==&gt; (4 + (2 * x))

     

     y    ==&gt; (2 * x)

          ==&gt; (2 * (4 + y))

     </pre>



<p>Each macro is expanded when it appears in the definition of the other

macro, but not when it indirectly appears in its own definition.



   </body></html>



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -