summaryrefslogtreecommitdiff
path: root/devdocs/gcc~13/function-names.html
blob: 56b881074c406b0092afe4fa5af6d5b76a2c9988 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<div class="section-level-extent" id="Function-Names"> <div class="nav-panel"> <p> Next: <a href="return-address" accesskey="n" rel="next">Getting the Return or Frame Address of a Function</a>, Previous: <a href="incomplete-enums" accesskey="p" rel="prev">Incomplete <code class="code">enum</code> Types</a>, Up: <a href="c-extensions" accesskey="u" rel="up">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div>  <h1 class="section" id="Function-Names-as-Strings"><span>6.50 Function Names as Strings<a class="copiable-link" href="#Function-Names-as-Strings"> ¶</a></span></h1>    <p>GCC provides three magic constants that hold the name of the current function as a string. In C++11 and later modes, all three are treated as constant expressions and can be used in <code class="code">constexpr</code> constexts. The first of these constants is <code class="code">__func__</code>, which is part of the C99 standard: </p> <p>The identifier <code class="code">__func__</code> is implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">static const char __func__[] = "function-name";</pre>
</div> <p>appeared, where function-name is the name of the lexically-enclosing function. This name is the unadorned name of the function. As an extension, at file (or, in C++, namespace scope), <code class="code">__func__</code> evaluates to the empty string. </p> <p><code class="code">__FUNCTION__</code> is another name for <code class="code">__func__</code>, provided for backward compatibility with old versions of GCC. </p> <p>In C, <code class="code">__PRETTY_FUNCTION__</code> is yet another name for <code class="code">__func__</code>, except that at file scope (or, in C++, namespace scope), it evaluates to the string <code class="code">"top level"</code>. In addition, in C++, <code class="code">__PRETTY_FUNCTION__</code> contains the signature of the function as well as its bare name. For example, this program: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">extern "C" int printf (const char *, ...);

class a {
 public:
  void sub (int i)
    {
      printf ("__FUNCTION__ = %s\n", __FUNCTION__);
      printf ("__PRETTY_FUNCTION__ = %s\n", __PRETTY_FUNCTION__);
    }
};

int
main (void)
{
  a ax;
  ax.sub (0);
  return 0;
}</pre>
</div> <p>gives this output: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">__FUNCTION__ = sub
__PRETTY_FUNCTION__ = void a::sub(int)</pre>
</div> <p>These identifiers are variables, not preprocessor macros, and may not be used to initialize <code class="code">char</code> arrays or be concatenated with string literals. </p> </div>  <div class="nav-panel"> <p> Next: <a href="return-address">Getting the Return or Frame Address of a Function</a>, Previous: <a href="incomplete-enums">Incomplete <code class="code">enum</code> Types</a>, Up: <a href="c-extensions">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div><div class="_attribution">
  <p class="_attribution-p">
    &copy; Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br>
    <a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Function-Names.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Function-Names.html</a>
  </p>
</div>