It is not redefined. But a different object is defined each time.
That is fine. As you probably already know, C++ allows more than one object to be defined.
As to the "why" they are different: Both the const and the unnamed namespace orthogonally give the variable internal linkage.
As I said: You would use it wherever you would have used a static
declaration in namespace scope.
Am 04.02.2013 11:15 schrieb "Klaim - Joël Lamotte" <mjk...@gmail.com>:
>
> Thank you both, this is very clear and helpful.
>
> Now may I ask if you know cases where anonymous namespace in a header
> is very useful?
> The first thing I think about is constants definitions in one header instead of cpp+header, but I don't really see an advantage.
>
Unnamed namespaces should only be used in a header for data (but i still prefer just declaring them "const"). And only when the data in it is const.
Mutable data wont carry modifications outside a TU which is bad if the declaration was in a header.
And for functions, making them static or in an unnamed namespace easily will bloat the executable. Either make it inline, so that you have only one function. Or define it in the cpp file (which we want to avoid for the purpose of discussion).
And for classes, the reasoning is the same as fir nonconst data. If they are ina header, they ought to have extern linkage and be the same for every inclusion. Otherwise, put it not in a header.
Another good argument for not splitting const data up into a cpp file is avoiding the static initialization order fiasco that happens when you use the data value in the initialization of another nonlocal nonmember object.
Unnamed namespaces should only be used in a header for data (but i still prefer just declaring them "const"). And only when the data in it is const.
Mutable data wont carry modifications outside a TU which is bad if the declaration was in a header.
And for functions, making them static or in an unnamed namespace easily will bloat the executable. Either make it inline, so that you have only one function. Or define it in the cpp file (which we want to avoid for the purpose of discussion).
And for classes, the reasoning is the same as fir nonconst data. If they are ina header, they ought to have extern linkage and be the same for every inclusion. Otherwise, put it not in a header.
Another good argument for not splitting const data up into a cpp file is avoiding the static initialization order fiasco that happens when you use the data value in the initialization of another nonlocal nonmember object.
Unlike static variables, anonymous namespaces do not affect linkage.
This means that you can have variables and functions with external
linkage in anonymous namespaces, which lets you use their addresses as
template arguments.
An unnamed namespace or a namespace declared directly or indirectly within an unnamed namespace has
internal linkage. All other namespaces have external linkage. A name having namespace scope that has not
been given internal linkage above has the same linkage as the enclosing namespace if it is the name of:
...variable, function, named class, etc...