Re: #include within namespace scope
On Feb 16, 4:18 pm, 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.
Part of the problem may be that the library is doing just that:
using ::std::, instead of just std::. So <vector> includes
something like:
namespace std
{
namespace _Private_ {
class _UsedByVector { /* ... */ } ;
}
template< ... >
class vector
{
// ...
::std::_Private_::_UsedByVector ...
} ;
}
When you wrap this in namespace evil,
::std::_Private_::_UsedByVector isn't defined, only
::evil::std::_Private_::_UsedByVector.
It's because of such things that the standard makes the
requirement that standard headers may not be included in
declarations or definitions.
--
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