Boost logo

Boost :

From: James Dennett (jdennett_at_[hidden])
Date: 2001-11-25 12:06:05


Jeff King wrote:

>On Sun, 25 Nov 2001, John Maddock wrote:
>
>>Yes, the problem is when two headers included by the same translation unit
>>use namespace scope static assertions - if the assertions are on the same
>>line in their header files, and in the same namespace, then some compilers
>>complain, others generate warnings, but otherwise accept the code. It's
>>easier to suggest a unique namespace, than to try to explain why in some
>>really obsure case the code stops working...
>>
>
>Err...right. So you have the obscure (but possible) problem of
>accidentally declaring the exact same "unique" namespace in two files
>(which might be more common across a large project). Isn't solving that
>the entire purpose of the anonymous namespace?
>
>You could even declare a STATIC_ASSERT that put itself in the anonymous
>namespace automagically, but that means a different STATIC_ASSERT macro
>for namespaces (since you can't open a namespace in function or class
>scope, IIRC).
>
>My point is to suggest:
>
>Why not use the anonymous namespace, which actually provides a unique
>name for you? It seems simpler to explain, and lets the compiler do the
>work of coming up with a unique name (which is guaranteed to work!).
>Is there some reason I don't know of not to recommend the anonymous
>namespace for this?
>
I think the point was that there's only a single top-level anonymous
namespace per translation unit, not per file. So, static asserts in
header files may collide if they're included in the same source file,
even if they're in an anonymous namespace.

-- James Dennett


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