Namespaces And Unqualified Name Lookup
by stl » Wed, 12 May 2004 19:09:24 GMT
gcc 3.3.3 and MSVC 7.1 disagree on whether a snippet of code is valid,
and I can't tell from the Standard which one is correct. I believe
that gcc 3.3.3 correctly accepts the code and MSVC 7.1 incorrectly
rejects it.
The code:
namespace meow {
struct mlar { };
}
namespace oink {
using namespace meow;
void fxn(mlar s);
}
void oink::fxn(mlar s) { }
Basically, when a function (fxn) lives in one namespace (oink) that
uses another namespace (meow), can the function's arguments (s) use
unqualified types (mlar), instead of qualified types (meow::mlar)?
I looked at 14882:2003 3.4.1 (6), but I couldn't understand how to
apply it to this example.
Explicit qualification satisfies both gcc 3.3.3 and MSVC 7.1.
Both compilers accept the code when the mlar type lives in the oink
namespace itself, and not a namespace that the oink namespace uses:
namespace oink {
struct mlar { };
void fxn(mlar s);
}
void oink::fxn(mlar s) { }
It seems that explicit qualification is needed on return types; that
makes sense to me, since the function's name hasn't been seen yet.
The question is whether explicit qualification is needed on the
arguments (this also applies to types in exception specifications).
Stephan T. Lavavej
http://www.**--****.com/
[ See http://www.**--****.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Re: Namespaces And Unqualified Name Lookup
by llewelly » Fri, 14 May 2004 09:22:11 GMT
XXXX@XXXXX.COM (Stephan T. Lavavej) writes:
The example in 3.4.1/6 alone is not sufficient to cover this
case. You need to combine it with 7.3.4/1 :
# [...] During unqualified name lookup (3.4.1), the names appear
# as if they were declared in the nearest enclosing namespace
# which contains both the using-directive and the nominated
# namespace. [...]
So I believe gcc 3.3.3 is correct to accept the code, and MSVC 7.1
wrong to reject it.
[snip]
Are you sure? I don't see that in the standard, and both como online
and gcc 3.[34] accept:
namespace meow {
struct mlar { };
}
namespace oink {
using namespace meow;
mlar fxn(mlar s); //no qualifcation on return type.
}
meow::mlar oink::fxn(mlar s) { }
[ See http://www.**--****.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Re: Namespaces And Unqualified Name Lookup
by stl » Sun, 16 May 2004 00:07:39 GMT
[llewelly]
There's no argument about the prototype inside the "namespace oink"
block - the using directive is sitting right on top of it, so you can
omit all qualifications. The question is what happens with the
definition below (and note, you do qualify the return type there).
Since you say Comeau and 3.4 accept the omitted qualification on the
argument in the definition, I'm more inclined to believe that MSVC is
incorrectly rejecting the code, but I'd still like to know what part
of the Standard says this.
[ See http://www.**--****.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Re: Namespaces And Unqualified Name Lookup
by llewelly » Sun, 16 May 2004 09:46:23 GMT
XXXX@XXXXX.COM (Stephan T. Lavavej) writes:
I didn't think about the definition. That's why it's return type was
still qualified in my example.
Comeau and g++ 3.4 accept the omitted qualification on the return
type *only* if the definition is moved into the namespace oink,
like this:
namespace meow {
struct mlar { };
}
namespace oink {
using namespace meow;
mlar fxn(mlar s);
mlar fxn(mlar s) { }
}
This:
namespace meow {
struct mlar { };
}
namespace oink {
using namespace meow;
mlar fxn(mlar s);
}
mlar oink::fxn(mlar s) { }
is rejected by como and gcc 3.4.
I can't figure out why omitted the qualification on the return type
when the definition is outside the namespace oink is being
rejected. I keep looking at 7.3.4/1 and wondering what I am
missing:
# [...] During unqualified name lookup (3.4.1), the names appear
# as if they were declared in the nearest enclosing namespace
# which contains both the using-directive and the nominated
# namespace. [...]
Again, I think 7.3.4/1 requires your original example be accepted. On
the other hand, I also think 7.3.4/1 requires my 2nd example with
the unqualified return type outside the namespace to be accepted
as well, since it is inside the nearest enclosing namespace which
contains both the using-directive and the nominated
namespace. But como and gcc 3.4 still reject it. Am I wrong?
[ See http://www.**--****.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Similar Threads:
1.Unqualified name lookup doubt (ISO/IEC-14882:2003 3.4.1/13)
Hi,
[I posted this in comp.std.c++ but the post never appeared. So trying
here]
ISO/IEC 14882:2003 Section 3.4.1/13 has the following
[...] Names declared in the outermost block of the function definition
are not found when looked up in the scope
of a handler for the function-try-block. [Note: but function parameter
names are found. ]
I thought the following example illustrated the above point but all
the compilers (gcc 3.4.2, MS VC++ 2005, Comeau online compiler) I
tried it with accept the code without any errors.
int main()
{
int x;
try {
// ...
}
catch(...) {
int i = x; // Should lookup of 'x' fail here???
//...
}
return 0;
}
Please explain what the above sentence from 3.4.1/13 really implies
(possibly with a small code example).
Thanks,
Murali
2.Default arguments and unqualified name lookup
3.Cross-namespace name lookup
Is the code below ill formed (because operator >> is defined in different
namespace than class B)?
It fails with VS 2005 Beta. I don't know if I should redesign my code or if
I should find a better compiler?
namespace N1
{
class A { };
std::istream &operator >>(std::istream &, A &);
}
namespace N2
{
class B: public A { };
}
void f()
{
A a;
B b;
cin >> a; // OK
cin >> b; // Error: cannot find a matching operator >> for class B
}
4.templates, namespace, and name lookup
5.name lookup and unnamed namespaces
6. qualified name and unqualified name
7. qualified name VS unqualified name in class template.