From: Martin B. on
Is an anonymous / unnamed namespace unique to the translation unit or is
it simply unique, that is, is the following legal:

*** file.cpp ***
// ...
namespace {
int HelperFn(double d);
};
// ...
void ExternalFn() {
// ...
int x = HelperFn(1.0);
// ...
}
// ...
namespace { // Is this the *same* anonymous namespace as above?
int HelperFn(double d) {
// ...
return l;
}
};
*** ***

It works on VC++2005 but is it std behaviour?

cheers,
Martin

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: cpp4ever on
On 07/01/2010 03:54 PM, Martin B. wrote:
> Is an anonymous / unnamed namespace unique to the translation unit or is
> it simply unique, that is, is the following legal:
>
> *** file.cpp ***
> // ...
> namespace {
> int HelperFn(double d);
> };
> // ...
> void ExternalFn() {
> // ...
> int x = HelperFn(1.0);
> // ...
> }
> // ...
> namespace { // Is this the *same* anonymous namespace as above?
> int HelperFn(double d) {
> // ...
> return l;
> }
> };
> *** ***
>
> It works on VC++2005 but is it std behaviour?
>
> cheers,
> Martin
>

The contents of an anonymous namespace are visible only to code in the
same source file/unit, hence using anonymous namespace in a header file
is pointless, and can cause code bloat. Your example only works if they
are part of the same code file/unit. If you try accessing the HelperFn()
function from another source file/unit it should fail. Effectively
anonymous namespace restricts the scope of it's contents to the
file/unit in which it appears.

HTH

cpp4ever


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Chris Uzdavinis on
On Jul 1, 9:54 am, "Martin B." <0xCDCDC...(a)gmx.at> wrote:
> Is an anonymous / unnamed namespace unique to the translation unit or is
> it simply unique, that is, is the following legal:

It is unique per translation unit. For example, if you have a header
that defines a variable in the anonymous namespace and you include
that header in several translation units, you will not have violations
of the one definition rule. You will have N distinct variables, each
with the "same" spelling of its name but unique to the file that
included it.

Think of the anonymous namespace as a named namespace, but it's the
compiler that invisibly generates the unique name for it (and
qualifies all access to it with that name) differently for each
translation unit. It does this in such a way that users cannot spell
the name given to a translation unit, since that's all managed by the
compiler.

Chris


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel Krügler on
On 1 Jul., 16:54, "Martin B." <0xCDCDC...(a)gmx.at> wrote:
> Is an anonymous / unnamed namespace unique to the translation unit or is
> it simply unique, that is, is the following legal:
>
> *** file.cpp ***
> // ...
> namespace {
> int HelperFn(double d);};
>
> // ...
> void ExternalFn() {
> // ...
> int x = HelperFn(1.0);
> // ...}
>
> // ...
> namespace { // Is this the *same* anonymous namespace as above?
> int HelperFn(double d) {
> // ...
> return l;
> }};
>
> *** ***
>
> It works on VC++2005 but is it std behaviour?

The C++ standard guarantees that *all* occurences
of the same unnamed namespace within the same
translation unit do have the same identifier, see
C++03, 7.3.1.1 [namespace.unnamed]/1:

"An unnamed-namespace-definition behaves as if it
were replaced by

namespace unique { /* empty body */ }
using namespace unique;
namespace unique { namespace-body }

where all occurrences of unique in a translation unit
are replaced by the same identifier and this identifier
differs from all other identifiers in the entire program."

Thus you can rely on that behaviour.

HTH & Greetings from Bremen,

Daniel Kr�gler


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Paul Bibbings on
"Martin B." <0xCDCDCDCD(a)gmx.at> writes:

> Is an anonymous / unnamed namespace unique to the translation unit or
> is it simply unique, that is, is the following legal:
>
> *** file.cpp ***
> // ...
> namespace {
> int HelperFn(double d);
> };
> // ...
> void ExternalFn() {
> // ...
> int x = HelperFn(1.0);
> // ...
> }
> // ...
> namespace { // Is this the *same* anonymous namespace as above?
> int HelperFn(double d) {
> // ...
> return l;
> }
> };
> *** ***
>
> It works on VC++2005 but is it std behaviour?

As given in [namespace.unnamed], �7.3.1.1/1:

"An /unnamed-namespace-definition/ behaves as if it were replaced by
namespace unique { /* empty body */ }
using namespace unique;
namespace unique { namespace body }

where all occurences of /unique/ in a translation unit are replaced
by the same identifier and this identifier differs from all other
identifiers in the entire program."

I understand the "as if replaced by" example to indicate that, within
the same translation unit, an unnamed namespace (namespace unique, here)
can be reopened in just the same way that named namespaces can, and the
addition of "where *all* occurences of /unique/ in a translation
unit..." appears (to me) to settle the "as if" interpretation of your
code to be:
*** file.cpp ***
namespace unique { }
using namespace unique;
// ...
namespace unique {
int HelperFn(double d);
};
// ...
void ExternalFn() {
// ...
int x = HelperFn(1.0);
// ...
}
// ...
namespace unique {
int HelperFn(double d) {
// ...
return l;
}
};
Regards

Paul Bibbings


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]