Boost logo

Boost :

From: Richard Day (richardvday_at_[hidden])
Date: 2007-06-14 10:04:31


Guillaume Melquiond wrote:
> Le samedi 09 juin 2007 à 18:43 -0400, Gennadiy Rozental a écrit :
>
>>>> Can anyone explain why unnamed namespace is a problem?
>>>>
>>> Inline functions referring to the content of an unnamed namespace will
>>> break the one-definition rule, when their header is included several
>>> times. Undefined behavior ensues.
>>>
>> I don't mean in general. static vars have the same issue. I mean why is it
>> reported by our tools?
>>
>
> I don't mean in general either. The example I give you is based on your
> very own files. They contain unnamed namespaces used from inline
> functions, hence they are not compliant with the C++ standard.
>
> For example, see the token_iterator.hpp file detected by the inspection
> reports. The keep_empty_tokens variable is defined in an unnamed
> namespace but used in an inline function, so the one that is passed as
> an argument to the function may not be identical to the one the function
> tests for.
>
> Not that it may change anything, but this is not standard C++, which is
> a bit unfortunate for a Boost library. Moreover, it artificially
> increases the linking time and the size of the execution files for no
> good reason.
>
> Best regards,
>
> Guillaume

Looking at the inspection notifications it seems unnamed namespaces are
in fact one of if not in fact the largest real (Broken links aside)
offender.
I have used them sometimes but only in source files not in
headers(Unless you can GUARANTEE it will only be included once. Doubtful).
I only use them when they are intended to be private to the file in
question.
When I don't want someone to just be able to extern it and reference
from another file for example.
But there has to be a compelling reason to do so.

I only use them for constants or classes, no statics as thats what the
unnamed namespace is for in the first place IMO. No globals either.
Globals are another discussion entirely but I digress.

Basically if boost is striving for header only libraries when ever
possible(And that is my impression) should there be unnamed namespace's
being used at all ?

Best Regards,
    Richard Day


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk