Search code examples
c++namespacesinitializationheader-filesunnamed-namespace

Are there any uses for unnamed namespaces in header files?


Someone asserted on SO today that you should never use unnamed namespaces in header files. Normally this is correct, but I seem to remember once someone told me that one of the standard libraries uses unnamed namespaces in header files to perform some sort of initialization.

Am I remembering correctly? Can someone fill in the details?


Solution

  • The only situation in which a nameless namespace in header can be useful is when you want to distribute code as header files only. For example, a large standalone subset of Boost is purely headers.

    The token ignore for tuples, mentioned in another answer is one example, the _1, _2 etc. bind placeholders are others.