Is the following C++ code valid?
namespace Foo
{
class Bar
{
// Class code here.
};
}
namespace Foo
{
namespace Bar
{
void SomeFunction();
{
// Function code here.
}
}
}
In other words, can there be a namespace with the same name as a class?
You cannot have the arrangement you have in your question because there is no way to disambiguate
Bar
.My compiler says:
"can there be a namespace with the same name as a class?"
No, If they are in the same namespace, as in your case.
Otherwise, yes. Anything can have the same name as anything else if they are in different namespaces. See this stackoverflow thread as reference.
No, but you can have
SomeFunction
be a static member of theBar
class.The result is not 100% equivalent to what you want (because of ADL) but the qualified names are what you expect.