Topic: Are anonymous namespaces closed?
Author: "B. James Phillippe" <bryan@terran.org>
Date: 1998/05/24 Raw View
Hello,
In Stroustrup's C++ 3rd Ed., he mentions that the standards
committee has deprecated the use of keyword 'static' to limit a
declaration/definition to file (translation unit) scope. Instead, he
advises the use of anonymous namespaces. Earlier in the text, he mentions
that namespaces are open, in that declarations/definitions may be added at
any point in time by "reopening" the namespace with "namespace <name>".
These factors combined have lead me to design the following style of code:
---some_file.cc---
namespace {
void some_function();
}
int main( int, char*[] ) {
// ...
some_function();
// ...
}
namespace {
void some_function() { /* ... */ }
}
---end file---
However, my compiler will not link this. I was then told that each
anonymous namespace was unique, which implies that anonymous namespaces are
closed. Is this true? If so, doesn't that suck? This forces us to
forward-define all functions if we wish to avoid using 'static'; like this:
---some_file.cc---
namespace {
void some_function() { /* ... */ }
}
int main( int, char*[] ) {
// ...
...
---end file---
This would seem to detract from style of coding much more than the use of
'static'! Can anyone definitively advise me on what is the correct
behavior of anonymous namespaces? I am hoping that they are open for the
scope of the entire translation unit.
thanks,
-bp
--
B. James Phillippe <bryan@terran.org>
Linux Software Engineer, WGT Inc.
http://earth.terran.org/~bryan
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: stephen.clamage@sun.com (Steve Clamage)
Date: 1998/05/24 Raw View
In article 100000@earth.terran.org, "B. James Phillippe" <bryan@terran.org> writes:
>
> In Stroustrup's C++ 3rd Ed., he mentions that the standards
>committee has deprecated the use of keyword 'static' to limit a
>declaration/definition to file (translation unit) scope. Instead, he
>advises the use of anonymous namespaces. Earlier in the text, he mentions
>that namespaces are open, in that declarations/definitions may be added at
>any point in time by "reopening" the namespace with "namespace <name>".
>These factors combined have lead me to design the following style of code:
>
>---some_file.cc---
>
>namespace {
> void some_function();
>}
>
>int main( int, char*[] ) {
>
> // ...
> some_function();
> // ...
>}
>
>namespace {
> void some_function() { /* ... */ }
>}
>
>---end file---
>
>However, my compiler will not link this. I was then told that each
>anonymous namespace was unique, which implies that anonymous namespaces are
>closed. Is this true?
No, your compiler is wrong. The unnamed namespace acts like it has
a unique name in each different compilation unit, but the same
unique name within one compilation unit. Reference: draft standard
section 7.3.1.1, "Unnamed namespaces".
Your code is OK as you show it.
---
Steve Clamage, stephen.clamage@sun.com
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]