Interpretation of [basic.scope.hiding]p2 when unqualified name lookup involves using-directives

11 views
Skip to first unread message

Anders Granlund

unread,
Jul 29, 2015, 8:15:36 AM7/29/15
to ISO C++ Standard - Discussion
Hi,

There are two types of name hiding in c++:

1) Normal name hiding: [basic.scope.hiding]p1 http://eel.is/c++draft/basic.scope.hiding#1
2) The special type of name hiding in [basic.scope.hiding]p2 http://eel.is/c++draft/basic.scope.hiding#2

I'm interested to know about how name hiding interacts with using-directives when unqualified name lookup is performed.

For the first type of name hiding the behaviour is quite clear. This is because [basic.scope.hiding]p1 has been reformulated in terms of the rules in the section [basic.lookup.unqual] http://eel.is/c++draft/basic.lookup.unqual

The same has not been done for the second type of name hiding. So the following question now arises:

*) How should this second type of name hiding interact with unqualified name lookup that involves using-directives?

Looking elsewhere in the standard I find [namespace.udir]p2 http://eel.is/c++draft/namespace.udir#2 and I think this is key to answering this question:

"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 ([basic.lookup.unqual]), the names appear as if they were declared in the nearest enclosing namespace which contains both the using-directive and the nominated namespace. [ Note: In this context, “contains” means “contains directly or indirectly”. — end note ]"

Applying the as if part of this rule to [basic.scope.hiding]p1 gives consistency with the rules in the section [basic.lookup.unqual]. This application is also consistent with [basic.scope.hiding]p4 http://eel.is/c++draft/basic.scope.hiding#4 So this looks promising.

Because of this I think we can answer the question *) by similarly applying the as if part of [namespace.udir]p2 to [basic.scope.hiding]p2. This application is also consistent with [basic.scope.hiding]p4. I think this is also the most natural and least complex interpretation of the c++ standard. 

The problem however is that Clang and GCC does not make the same interpretation as me. For example:

namespace N { static int i = 1; }
namespace M { struct i {}; }
using namespace M;
using namespace N;

int main() { sizeof(i); }

According to my interpretation this program should be well-formed and i should be looked up as the integer variable. Both Clang and GCC disagree with this by giving a name lookup ambiguity.

In the case of Clang this more complex interpretation leads to the following bug:

namespace N { static int i = 1; }
  namespace M { struct i {}; }

  namespace P {
      using N::i;
      using M::i;
  }

  namespace Q { using M::i; }

  using namespace P;
  using namespace Q;

  int main() { sizeof (i); }

Gives no errors, but change

  using namespace P;
  using namespace Q;

into 

  using namespace Q;
  using namespace P;

and we get name-lookup ambiguity error. GCC is at least consistent here.

What I want with this post is to get a second option about the way I interpret the c++ standard since at least two compilers seems to disagree with me (even if one of the is obviously buggy in its interpretation of the standard).

/Anders

Anders Granlund

unread,
Jul 29, 2015, 12:42:02 PM7/29/15
to std-dis...@isocpp.org
I got the answer on stack overflow now: http://stackoverflow.com/questions/31702956/interpretation-of-basic-scope-hidingp2-when-unqualified-name-lookup-involves-u

Turned out that there are other rules preventing making my interpretation. This is the reason I don't like "as if" rules. It is not always clear how far you can go in applying them. You apply them, and then after a while you see some other rule that prevent that application.

Also I think the section [basic.lookup.unqual] could include a reformulation of [basic.scope.hiding]p1 http://eel.is/c++draft/basic.scope.hiding#1 .


--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussio...@isocpp.org.
To post to this group, send email to std-dis...@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-discussion/.

Reply all
Reply to author
Forward
0 new messages