List of C++ name resolution (and overloading) rules
Asked Answered
P

1

43

Where I can find a list of the rules that a C++ compliant compiler must apply in order to perform names resolution (including overloading)?

I'd like something like a natural-language algorithm or flow chart.

C++ standard of course has this set of rules but it is build up as new language statements are introduced and the result it's pretty hard to remember.

To make a long story short, I'd like to know the complete and detailed answer to the question "What compiler do when it see the name 'A'?"

I know C++ is all "We do this when X but not Y if Z holds" so, I'm asking whether it is possible to make it more linear.

EDIT: I'm working on a draft of this topic, something that may be improved collectively once posted. However i'm very busy this days and it may take time to have something publicable. If someone interested i'll promote the "personal note on a raw txt file" to something better and post it.

Palinode answered 10/9, 2011 at 20:43 Comment(6)
Appendix B of C++ Templates - The Complete Guide is about overload resolution. "In this appendix we provide a reasonably detailed survey of the overload resolution rules. However, the complexity of this process is such that we do not claim to cover every part of the topic."Stephanotis
Indeed, the rules in the standard are spread and very highly coupled throughout the document.Zischke
I'm going to add a bounty to this question whenever it becomes available, I'd love to have a serious reference of names lookup and overload resolution rules more readable than the standard.Eboni
I would like to point out that the process is separated in two distinct phases. 1. Name Lookup / 2. Overload resolution. The question already being broad, I feel it would be more conclusive if it was split in two. They are (anyway) applied independently I think, overload resolution occurring only on the set of looked up names.Stelmach
@Matthieu M. That's true. Overload acts on results from name resolution (although name lookup ends in the scope where the very first, of possible multiple, name is found). However my intentions was to have a single (big) figure of both.Palinode
@knm241: since they do not influence each other I think you'll have a much better chance of getting answers if you split the question. It might even be worth a C++-faq, though that should be asked in the lounge first.Stelmach
D
12

Well, in broad strokes:

  • If the name is preceded by ::, as in ::A or X::A, then use qualified name lookup. First look up X, if it exists (if not use the global namespace) then look inside it for A. If X is a class, and A is not a direct member, then look in all the direct bases of X. If A is found in more than one base, fail.

  • Otherwise, if the name is used as a function call such as A( X ), use argument-dependent lookup. This is the hard part. Look for A in the namespace the type of X was declared in, in the friends of X, and if X is a template instantiation, likewise for all the arguments involved. Scopes associated only by typedef do not apply. Do this in addition to unqualified lookup.

  • Start with unqualified lookup if argument-dependent lookup doesn't apply. This is the usual way variables are found. Start at the current scope and work outwards until the name is found. Note that this respects using namespace directives, which the other two cases do not.

Simply glancing at the Standard will reveal many exceptions and gotchas. For example, unqualified lookup is used to determine whether the name is used as a function call, as opposed to a cast-expression, before ADL is used to generate a list of potential overloads. Unqualified lookup doesn't look for objects in enclosing scopes of nested of local classes, because such objects might not exist at the time of reference.

Apply common sense, and ask more specific questions when (as often it does) intuition fails.

Divorcement answered 11/9, 2011 at 3:56 Comment(4)
Alas this is far from being a complete picture of the whole argument. A note: you written "then look in all the direct bases of A", did you mean "then look in all the BASES of A"?Palinode
@knm: I mean, start with the immediate base classes of A, then for each base, recurse to its bases. If any two branches of this search find X, then the process fails.Divorcement
@knm241: Patched, X is the class (scope) being searched and A is the element being searched.Stelmach
Base classes of base classes of class X are indirect base class of X. According to the standard :) Again, thank you all for spending time answering this question.Palinode

© 2022 - 2024 — McMap. All rights reserved.