Unnamed Namespaces Versus Static/Local Functions

Tom Browder tom.browder@gmail.com
Tue Apr 27 10:31:00 GMT 2010


This question has been asked before, but the answer was somewhat incomplete.

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()

If the standard says one can't do that, then isn't the standard
incomplete in its use of unnamed namespaces?  How many folks will
change from using static in that case?  Or is there another
work-around other than using a unique namespace name?

Thanks,

-Tom

Thomas M. Browder, Jr.
Niceville, Florida
USA



More information about the Gcc-help mailing list