This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Questions about unnamed namespace
- To: egcs at cygnus dot com
- Subject: Questions about unnamed namespace
- From: eyal dot ben-david at aks dot com
- Date: Tue, 6 Oct 1998 20:39:37 +0200
(I'm using EGCS 1.1 - mingw32 version)
Question 1
~~~~~~~~~~
I had a source file with many static functions. I decided to follow
the modern approach and used unnamed namespace for those functions and
variables. As a result I had some linkage problems.
Here is a small program that demonstrates the problem:
// Example
#include <iostream>
namespace
{
int add_xx(int n)
{
extern int xx;
return xx + n;
}
int xx = 10;
}
int main()
{
std::cout << add_xx(3) << "\n";
}
// END example
The linker does not find 'xx'.
Does 'extern' inside a function imply global scope or this is a
bug in EGCS ?
Notes:
1. if the extern declaration is moved out of the function the program
links OK.
2. Borland C++Builder accepts both forms.
Question 2
~~~~~~~~~~
In an older implementation I had a file with many small static
functions. The compiler expanded all these functions inline and
no code for these functions was written in the assembly listing.
After replacing the static with unnamed namespace block, the functions
were expanded inline but code for them was written in the assembly file
and hence the obj file and probably in the executable (I'm not sure
about the last since the linker can eliminate unused functions).
Is there any reason why the compiler (EGCS 1.1) behaves this way ?
Eyal.