Re: Forward declarations and namespaces
John wrote:
Victor Bazarov wrote:
John wrote:
[..]
== file1. hpp ==
namespace A
{
class X
{
...
};
}
Method 1:
== file2. hpp ==
class A::X;
namespace B
{
class Y
{
A::X* x;
};
}
[..]
When #include "file1.hpp" is not included in file2.hpp, what meaning
does
class A::X;
have?
As I understand it, it's an error since 'A' is unknown.
V
I'm not trying to be nit-picky but I'm trying to figure out why MS
Visual Studio is compiling this code with no warnings. Would the
following be proper (this is actually what the original code has)?
== file2. hpp ==
// somefile.hpp declares namespace A, but not X
#include "somefile.hpp"
using namespace A;
class A::X;
namespace B
{
class Y
{
A::X* x;
};
}
No, it wouldn't. First off, you can always test your code without
splitting it into headers and sources. Just put the contents of your
header in your source, verbatim:
namespace A {} // instead of explaining what blah.hpp does
class A::X;
namespace B
{
class Y
{
A::X* x;
};
}
Second, there are other compilers available for testing some concepts
which you are investigating. There is GCC (in MinGW form), there is
Comeau online.
In this particular case, the declaration of 'A::X' still looks 'X' up in
the namespace A (which it finds), and *fails* since there is no 'X' in
'A'. The reason it tries to find 'X' is because when it sees the name
(once the token 'X' has been read from the stream, just before
processing the semicolon), it doesn't know that you're *not* trying to
*define* the class, but merely to declare it (well, that's what you hope
to accomplish, isn't it?). So, it looks the name up, can't find it, and
ought to complain.
The rule is simple: declarations are declarations (whether you prepend
them with "forward" or "backward" or "sideways"). They *declare* a name
(and that's why the name should appear by itself in the declaration,
naked, without qualifications). And the declarations are only therefore
valid at the exact point where the name should originally be declared,
so to speak. IOW if you intend for 'X' to be declared in 'A', declare
it *there*.
Now, I don't know why VC++ compiles erroneous code. IME VC++ is too lax
WRT to language rules about what is and what isn't legal code (something
about the level of the users of that compiler, I am guessing, on
average). Try disabling extensions (/Za option, IIRC). Further
inquiries about VC++ specifics should be made in the VC++ newsgroup
(microsoft.public.vc.langauge).
Good luck!
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask