This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Unnamed Namespaces Versus Static/Local Functions
Hi Tom,
On Mon, Apr 26, 2010 at 07:18:58PM -0500, Tom Browder wrote:
> This question has been asked before, but the answer was somewhat
> incomplete.
Sorry, I haven't found the old discussion;-(
>
> Isn't a function declared in an anonymous namespace supposed to be
> able to be defined later in the same translation unit? It works for
> variables; For example:
>
> // file 1 // errors
> #include <iostream>
> namespace {
> void foo();
> int zip;
> }
> void foo() {}
> int main() {
> //foo(); // this statement generates an error
> zip = 3; // this doesn't
> cout << "zip = " << zip << "\n";
> }
>
> // error message (g++-4.5.0):
> nspace.cc: In function âint main()â:
> nspace.cc:15:7: error: call of overloaded âfoo()â is ambiguous
> nspace.cc:10:6: note: candidates are: void foo()
> nspace.cc:6:8: note: void<unnamed>::foo()
You CAN do it - but the definition has to be again inside the namespace:
#include <iostream>
using namespace std;
namespace {
void foo();
int zip;
}
namespace{
void foo() {}
}
int main() {
foo();
zip = 3;
cout << "zip = " << zip << "\n";
}
compiles correctly with g++-4.5.
The way how you had written it, you declare two functions "foo" - one in
the anonymous namespace, and one outside the namespace. And then the
overload is ambiguous.
Axel