c++ singleton, strange error
<p>I'm implementing a thread-safe singleton. But this aspect (singleton &amp; thread-safe) is not part of my question.</p>

<p>Compare the two codes. Code 1:</p>

<pre><code>#include &lt;iostream&gt;
using namespace std;
class DataLocation {
private:
  DataLocation(std::string) {
  }
public:
  DataLocation&amp; getInstance() {
    std::string s = " ";
    static DataLocation instance(s);
    return instance;
  }
};
int main() {
}
</code></pre>

<p>and code 2: </p>

<pre><code>#include &lt;iostream&gt;
using namespace std;
class DataLocation {
private:
  DataLocation() {
  }
public:
  DataLocation&amp; getInstance() {
    static DataLocation instance();    
    return instance;
  }
};
int main() {
}
</code></pre>

<p>Code 1 compiles fine. Code 2 gives the following error:</p>

<pre><code>15_singleton.cpp: In member function ‘DataLocation&amp; DataLocation::getInstance()’:
15_singleton.cpp:15:34: error: cannot declare static function inside another function
     static DataLocation instance();    
                                  ^
15_singleton.cpp:16:12: error: invalid initialization of non-const reference of type ‘DataLocation&amp;’ from an rvalue of type ‘DataLocation (*)()’
     return instance;
            ^
</code></pre>

<p>From my point of view the only difference is that the private constructor has one, respectively zero parameters. </p>

<p>How can I help the compiler to understand that I'm not defining anything new, but I'm just calling the constructor? The compiler is able to understand it, when there is one parameter.</p>