Dominance (C++)

In the C++ programming language, dominance refers to a particular aspect of C++ name lookup in the presence of Inheritance. When the compiler computes the set of declarations to which a particular name might refer, declarations in very-ancestral classes which are "dominated" by declarations in less-ancestral classes are hidden for the purposes of name lookup. In other languages or contexts, the same principle may be referred to as "name masking" or "shadowing".

The algorithm for computing name lookup is described in section 10.2 [class.member.lookup] of the C++11 Standard. The Standard's description does not use the word "dominance", preferring to describe things in terms of declaration sets and hiding. However, the Index contains an entry for "dominance, virtual base class" referring to section 10.2.

Example without diamond inheritance
In the above example,  contains a reference to the name. However, the program as a whole contains four declarations of the name. In order to figure out which  is meant, the compiler computes an overload set containing all the declarations which are not hidden at the point of the call. The declaration of  at global scope is hidden by , and in turn   is hidden by. Thus the only declaration which is considered by overload resolution is  — and the result in this case is a diagnostic, because the call-site provides two arguments where   expects only one.

It is often surprising to new C++ programmers that the declaration of  dominates and hides all of the more-ancestral declarations, regardless of signature; that is,   dominates and hides the declaration of   even though the two member functions have very different signatures.

It is also important to observe that in C++, name lookup precedes overload resolution. If  had multiple overloads (for example   and  ), the compiler would choose between them at overload-resolution time; but during the name-lookup phase we are concerned only with choosing among the three scopes ,  , and. The fact that  would have been a better overload than   is not part of the compiler's consideration.

Example with diamond inheritance
In the above example, the compiler computes an overload set for  which contains both   and. The compiler produces a diagnostic indicating that the program is ill-formed because the name  is ambiguous.

Example with virtual inheritance
In this final example, the name  once again unambiguously refers to , because   hides the   declared in its   subobject. The Standard calls out this surprising case in an informative note (§10.2 paragraph 10):

"When virtual base classes are used, a hidden declaration can be reached along a path through the subobject lattice that does not pass through the hiding declaration. This is not an ambiguity."

Even if  itself were to inherit virtually from , there would be no ambiguity in name lookup. However, if  were to inherit non-virtually from   (i.e.,  ), then the name would again be ambiguated (between the  s declared in the two   subobjects).