Is there any functional or optimization difference between the following?
// SomeClass.cpp
#include "SomeClass.h"
#include "SomeOtherClassInSomeClassNamespace.h"
using namespace SomeClassNamespace;
SomeClass::SomeClass() {}
void SomeClass::SomeFunc()
{
// uses some stuff in SomeClassNamespace not defined in SomeClass.h
}
or
// SomeClass.cpp
#include "SomeClass.h"
#include "SomeOtherClassInSomeClassNamespace.h"
namespace SomeClassNamespace
{
SomeClass::SomeClass() {}
void SomeClass::SomeFunc()
{
// uses some stuff in SomeClassNamespace not defined in SomeClass.h
}
}
As long as the merged scopes resolve as expected, then No.
Runtime or binary size? No.
If you want build optimizations, then additional complexity will be introduced via
usingand/or by reopening the namespace.I don’t use the former because resolutions can be problematic.
I don’t use the latter because it’s easy to end up with new declarations.
The way I use it:
this is a little verbose, but fast to resolve definitions with their declarations, reduces chance of collisions, can reduce programmer errors, and could reduce binary size if the definitions in the anonymous namespace ends up being exported (in case you also use private implementations). Of course, it will also make sense to keep your internals (“some stuff in SomeClassNamespace not defined in SomeClass.h”) in the right namespace (assuming they are used in multiple TUs).