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.
It works this way:
class A { friend int main(int, char**); A() {} };
int main(int, char **) { A a; }
But it breaks when I need to put class A in a namespace:
namepace ns { class A { friend int main(int, char**); A() {} }; }
int main(int, char **) { ns::A a; }
The problem is scoping: the compiler now thinks
friend int main
means a function named main() in namespace ns. So the real main() becomes irrelevant.
So the question is: how to fix this? Of course I'll have to put class A in a namespace.