Just like we can declare in namespace and define in Global namespace, like this:
namespace B{
void func();
}
void B::func(){
std::cout<<"abcdef\n";
}
int main(){
B::func();
}
And we have to call it using B:: because its declared in B namespace as per aanswers to Forward declaring classes in namespaces.
void func();
namespace B{
void ::func(){
}
}
using namespace B;
int main(){
func();
}
And according to those answers, we can't do this because the compiler won't know where the definition is.
Is it safe to say the "Global namespace is special because compiler always checks it, that's why its safe to define in global namespace."?
Can we do something to make a user-defined namespace as special as the global namespace, or do something to make the compiler aware that the definition is "here!!"?
The global namespace encloses all other user-defined namespaces.
On the other hand, you may define a function declared in a namespace in its enclosing namespace.
From the C++ 20 (9.8.1.2 Namespace member definitions)
The first your program example is correct because the global namespace encloses the namespace B.
The second your program example is not valid because the user-defined namespace B does not enclose the global namespace where the function func is declared.
Here is an example. If you have the following function declaration within the namespace C
then it may be defined one of the following ways
or
or