BG> Is this valid C++ code?
BG>
BG> int qwe;
BG> int foo();
BG> class bar {
BG> public:
BG> int qwe;
BG> int ::foo() {return qwe;}
BG> };
According to my reading of the ISO C++ Standard, the answer is, surprisingly,
yes. (I was surprised, at any rate.) As far as I can see, the reference to
`qwe' in the function definition for `::foo' should refer to the name `qwe'
in global namespace scope, not to the name `qwe' in the scope of class `bar'.
I deduce this from the fact that none of the name lookup rules relating to
class scope in section 3 of the ISO C++ Standard apply to the names used in
definition of a function that *isn't* a member function.
The surprising thing is that a qualified declarator ID in the global
namespace scope seems to be allowed within a class definition. I cannot find
anything in the C++ Standard that disallows this (although I reserve the
right to find something after further investigation (-:). Although there's
nothing logically *wrong* with such a function definition, it does seem to be
a rather bizarre quirk of the language.
Certainly it is one that many implementors have overlooked. Several
compilers that I've tried will fail to compile this code.
¯ JdeBP ®
--- FleetStreet 1.19 NR
---------------
* Origin: JdeBP's point, using Squish (2:440/4.3)
|