Declare main() as friend function in C++
<p>I have a class that I only wish clients create them one object per process. Instead of singleton, a better way (I believe) is to tell the clients to only create them in main(). So a natural enforcement is to make the constructor private and main() as a friend.</p>

<p>It works this way:</p>

<pre><code>class A { friend int main(int, char**); A() {} };
int main(int, char **) { A a; }
</code></pre>

<p>But it breaks when I need to put class A in a namespace:</p>

<pre><code>namepace ns { class A { friend int main(int, char**); A() {} }; }
int main(int, char **) { ns::A a; }
</code></pre>

<p>The problem is scoping: the compiler now thinks </p>

<pre><code>friend int main 
</code></pre>

<p>means a function named main() in namespace ns. So the real main() becomes irrelevant.</p>

<p>So the question is: how to fix this? Of course I'll have to put class A in a namespace.</p>
