Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi,

I was wondering on peoples opinion's / best practices whether you think it is a good or bad thing to have a anonymous namespace in a header file.

Thanks for your input.
Posted

I would say that anonymous namespace in header file are generally evil.

In practice an unnammed namespace would generate a namespace with a unique compiler generated name for each source file.

Thus each source file that would include that header would see a different definition which is probably not what you intend.

As a general use, uses them only in source file if you want to avoid symbols to be visible to other source files.
 
Share this answer
 
v2
Comments
Christian Graus 4-Jul-11 21:28pm    
I was wondering if he was thinking of a case where all the source code is in the header file ?
Sergey Alexandrovich Kryukov 4-Jul-11 22:21pm    
I basically agree, my 5. Not using name spaces probably may fit the purpose when trying to quickly write a small application, such as a code sample for CodeProject.
--SA
bob_bobber2005 5-Jul-11 14:51pm    
Hi,

Thanks for your input. It was not me who wrote the class with a anonymous namespace in the header file (it's from source code at work on a very large system and I've only been there 2 weeks). I raised this question as I have not seen it before and so wanted other developers opinions about it.



Thanks.
It depends on the application. Anonymous namespaces, like many things in C++, are good tools if used well, and bad if used badly. Why would you decide to use an anonymous namespace ? Do you want to hide the contents ? The point of an anonymous namespace is that it can't be discovered and therefore cannot be used, as far as I recall.
 
Share this answer
 
Comments
ThatsAlok 5-Jul-11 8:19am    
agreed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900