It is good programming style to include all necessary dependencies in a header that references them. Often this includes declarations that are placed in the STD & global namespaces (like cstdio). However, this creates problems when a second programmer wants to wrap such an include file in a new namespace to encapsulate the first one.
With this scenario in mind, is there a way to force declarations into the global name space? As a conceptual example (this won't actually compile):
foo.h --> original definition
// force global namespace, this is redundant when foo is in the global space
// but could be important if foo included into another namespace.
namespace :: {
#include <cstdio>
}
namespace foo {
FILE *somefile;
}
bar.h --> New file, by a new programmer, encapsulating foo.
namespace bar {
# include "foo.h"
FILE *somefile; // bar:somefile, as opposed to bar::foo::somefile
}
Without the putative namespace ::{}
we end up with declarations like foo::FILE
, and foo::printf()
, when we just want ::FILE
and ::printf()
.
Popular libraries like boost resolve this by defining an explicit hierarchy. That would require that foo know it will be part of bar, and place global namespace includes outside of the local namespace definition.
However, good extensibility requires that we be able to use foo directly, OR wrap foo in a new namespace, without having to change foo or its includes.
Does anyone know how to accomplish this?