Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Name Look Behavior

53 views
Skip to first unread message

jghi...@gmail.com

unread,
Nov 19, 2014, 11:41:09 AM11/19/14
to
Consider:

namespace Outer {
typedef int Int32;
typedef char Char8;
}
using namespace Outer;

namespace Library {
namespace Inner {
typedef int Int32;
typedef char Char8;
}
using namespace Inner;

Int32 f() { // Outer::Int32 or Library::Inner::Int32?
Int32 x = 0;
return x;
}
}

An ambiguity would result if Inner::Int32 were defined at global scope, but
all the compilers I've used resolve the reference to Int32 in Library::f()
to Library::Inner::Int32. Intuition suggests that Library::Inner::Int32 is
favored over Outer::Int32 because it's declared in the Library's own namespace
(albeit a nested one). Is that reasoning in fact confirmed/required by
the standard? If so, is it also true (in this particular case) that the using
directive issued inside the Library is equivalent to a series of using
declarations, one for each name contained inside the Inner namespace (e.g.,
using Inner::Int32; using Inner::Char8; etc.)?

Charles J. Daniels

unread,
Nov 20, 2014, 2:33:37 AM11/20/14
to
I don't have the exact answer you seek, but the process is called "name lookup" and I really liked this video about it: http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Core-C-/Stephan-T-Lavavej-Core-C-1-of-n

Bix

unread,
Nov 20, 2014, 3:58:45 PM11/20/14
to
Hi,
I may be wrong but I think that the rule that happy is

7.3.4.2 [namespace.udir]
A using-directive specifies that the names in the nominated namespace can be used in the scope in which the
using-directive appears after the using-directive. 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 the unqualified lookup of Int32 foun Library::Inner::Int32.

cheers
bix

jghi...@gmail.com

unread,
Nov 20, 2014, 6:56:10 PM11/20/14
to
I read this line from the standard before posting my original question, but after re-reading it I believe it does indeed contain the answer to the question even though I didn't realize it at the time.

The Library namespace is the nearest namespace that encloses both the using directive and the namespace nominated by the using directive (i.e., Library::Inner). In this case, the names "appear as if" they were declared in the Library namespace and lookup can stop there.
0 new messages