Re: #include within namespace scope
On Feb 16, 2:27 pm, joec...@gmail.com wrote:
Is there anything that prevents putting #include directives
inside namespace scope within the standard?
Of course not. Include happens before namespaces are
recognized, and is pure textual inclusion. You can put one in
the middle of a function, if that's what you want.
The only restriction in the standard is with regards to the
standard includes; these must be included "outside of any
external declaration or definition." I'm not sure why the
particular exact wording, but namespaces and classes do count as
declarations, so the include of a standard header must be
outside of them.
Let's say I have a (evil) header:
header.h
#include <vector>
using namespace std; // bad doggy
class Doo
{
public:
vector<int> d;};
----------
and a test.cc:
namespace evil
{
#include "header.h"
}
int main()
{
// Nothing
}
gcc throws up all over the place. What rule am I violating?
You're including a standard header (indirectly) inside a
namespace.
More generally, since headers are textual inclusion, it's very
hard to write one which can really be included within a
namespace. The standard doesn't care, but there's a very large
possibility that the results will not be what you want (unless
the header only contains #define).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34