title: Is there any use for local function declarations?
body: <p>Most C++ programmers like me have made the following mistake at some point:</p>
  
  <pre><code>class C { /*...*/ };
  
  int main() {
    C c();     // declares a function c taking no arguments returning a C,
               // not, as intended by most, an object c of type C initialized
               // using the default constructor.
    c.foo();   // compiler complains here.
  
    //...
  }
  </code></pre>
  
  <p>Now while the error is pretty obvious once you know it I was wondering if there is any sensible use for this kind of local function declaration except that you can do it -- especially since there is no way to <em>define</em> such a local function in the same block; you have to define it elsewhere.</p>
  
  <p>I think that Java-style local classes are a pretty nice feature which I tend to use often, especially the anonymous sort. Even local C++ classes (which can have inline-defined member functions) have some use. But this local function declaration without definition thing seems very awkward to me. Is it just a C-legacy or is there some deeper use case which I am not aware of?</p>
  
  <p><strong>Edit</strong> for the non-believers: <code>C c()</code> is <em>not</em> a function pointer declaration.</p>
  
  <p>This program</p>
  
  <pre><code>int main()
  {
    void g();
    cout &lt;&lt; "Hello ";
    g();
    return 0;
  }
  
  void g()
  {
    cout &lt;&lt; "world." &lt;&lt; endl;
  }
  </code></pre>
  
  <p>outputs <code>Hello world.</code> This program</p>
  
  <pre><code>void fun()
  {
    cout &lt;&lt; "world." &lt;&lt; endl;
  }
  
  int main()
  {
    void g();
    g = fun;
    cout &lt;&lt; "Hello ";
    g();
    return 0;
  }
  </code></pre>
  
  <p>does not compile. gcc complains:</p>
  
  <pre>error: cannot convert 'void ()()' to 'void ()()' in assignment</pre>
  
  <p>comeau:</p>
  
  <pre>error: expression must be a modifiable lvalue</pre>