Declaring namespace as macro - C++
Probably not a best practice as it can be difficult to read compared to a vanilla namespace
declaration. That said, remember rules don't always apply universally, and I'm sure there is some scenario where a macro might clean things up considerably.
"But I couldn't find the STL files including this. If it is not included, how it can be used?".
All files that use this macro include yvals.h
somehow. For example <vector>
includes <memory>
, which includes <iterator>
, which includes <xutility>
, which includes <climits>
, which includes <yvals.h>
. The chain may be deep, but it does include it it some point.
And I want to clarify, this only applies to this particular implementation of the standard library; this is in no way standardized.
- In general No. The macros were probably used at the time when namespaces were not implemented by some compilers, or for compatibity with specific platforms.
- No idea. The file would probably be included by some other file that was included into the STL file.
One approach that I saw in a library that I recently used was:
BEGIN_NAMESPACE_XXX()
where XXX is the number of namespace levels for example:
BEGIN_NAMESPACE_3(ns1, ns1, ns3)
would take three arguments and expand to
namespace ns1 {
namespace ns2 {
namespace ns2 {
and a matching END_NAMESPACE_3
would expand to
}
}
}
(I have added the newlines and indentation for clarity's sake only)