Re: #include within namespace scope
joecook@gmail.com wrote:
On Feb 16, 10:18 am, joec...@gmail.com wrote:
On Feb 16, 9:10 am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
joec...@gmail.com writes:
Is there anything that prevents putting #include directives inside
namespace scope within the standard?
Nothing, but you will declare the stuff defined in the header inside
this namespace.
Let's say I have a (evil) header:
Therefore you have:
namespace evil
{
#include <vector>
using namespace std; // bad doggy
class Doo
{
public:
vector<int> d;
};
}
int main()
{
// Nothing
}
That is, you are using a class evil::std::vector<int>
Where is the implementation of this class?
Notice that libg++ only defines methods such as std::vector<int>::clear,
not evil::std::vector<int>::clear.
I expected the preprocessor to expand the include header at the
location I have put it.
I further expected that the compiler should always refer to ::std::
instead of std::, and therefore 'std' will not get caught inside the
namespace where it is not intended. Somewhere that expectation is
being broken, and I'm still not sure exactly where..
Joe C- Hide quoted text -
- Show quoted text -
To clarify,
The standard states, "Whenever a name x defined in the standard
library is mentioned, the name x is assumed to be fully qualified
as ::std::x, unless explicitly described otherwise. For example, if
the Effects section for library function F is described as calling
library function G, the function ::std::G is meant."
In this case, the compiler seems to be making the assumption that
include directives will always be at global scope, but I cannot find
any part of the standard that would back up such an assumption. That
is what I am searching for.
You cannot expect the compiler to *define* the namespace ::std *inside*
another namespace, it's just not allowed. If the headers only
*referred* to some elements, that would probably be OK, but since most
of the standard headers actually *define* classes (templates), you
should consider that every header contains a segment (so to speak) of
the ::std namespace:
------ header <vector>
namespace std {
template<class T, blahblah > class vector { blah ...
}
----------------
At this point the language does not allow *suspension* of the namespace
processing and switching to another one. If it were, we could have written
/// global namespace
namespace firstlevel { class atfirstlevel; }
namespace myown {
// note the two colons on the line below - not currently legal
namespace ::firstlevel { class onemoreatfirstlevel; }
class inside_myown;
} // namespace myown
Which should result in declaring class 'onemoreatfirstlevel' inside the
'::firstlevel' namespace. Since it doesn't exist, the declaration
namespace firstlevel
inside 'myown' opens *another*, nested, namespace.
Double colons before the namespace name in a namespace declaration are
*not allowed*.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask