C++ Logo

std-discussion

Advanced search

forward declaration inside another function in a namespace

From: Federico Kircheis <federico_at_[hidden]>
Date: Sat, 29 Jun 2024 14:34:58 +0200
Hello to everyone,

I am failing to understand if following code is really invalid.
I've looked at the standard, but I am missing where the relevant
information is.

Those snippets compile fine

1)
~~~~
namespace a {
   int bar(int);
   int foo() {
     return ::a::bar(12);
   }
   int bar(int) {
     return 1;
   }
}
~~~~
2)
~~~~
namespace a {
   int foo() {
     int bar(int);
     return bar(12);
   }
   int bar(int) {
     return 1;
   }
}
~~~~

but this one does not(!)

3)
~~~~
namespace a {
   int foo() {
     int bar(int);
     return ::a::bar(12);
   }
   int bar(int) {
     return 1;
   }
}
~~~~

both gcc and clang suggest that "'bar' is not a member of 'a'" / that
there is "no member named 'bar' in namespace 'a'" (replacing "::a" with
"a" does not change the outcome).

Since the function in namespace a is called (even if there is a bar
function in the global namescope), I am failing to understand why it
should not be possible to qualify the function call with the namespace.

Especially if there is a function at global scope, removing the forward
declaration changes the behavior of the program.


~~~~
int bar(int){
     return 0;
}

namespace a {
   int foo() {
     // if removed, the line after this comment calls ::bar,
     // and not ::a::bar
     // Since the function call is not qualified,
     // someone might even think it it already calling ::bar
     int bar(int num);
     return bar(12);
   }
}
~~~~

Best

Federico

Received on 2024-06-29 12:35:06