Re: Unnamed Namespaces Versus Static/Local Functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux