|
1442 | 1442 | the declaration of the \grammarterm{typedef-name} is discarded
|
1443 | 1443 | instead of the type declaration.
|
1444 | 1444 |
|
| 1445 | +\rSec2[class.member.lookup]{Member name lookup}% |
| 1446 | +\indextext{lookup!member name}% |
| 1447 | +\indextext{ambiguity!base class member}% |
| 1448 | +\indextext{ambiguity!member access} |
| 1449 | + |
| 1450 | +\pnum |
| 1451 | +A \defn{search} in a scope $X$ for a name $N$ from a program point $P$ |
| 1452 | +is a single search in $X$ for $N$ from $P$ |
| 1453 | +unless $X$ is the scope of a class or class template $T$, in which case the |
| 1454 | +following steps define the result of the search. |
| 1455 | +\begin{note} |
| 1456 | +The result differs only |
| 1457 | +if $N$ is a \grammarterm{conversion-function-id} or |
| 1458 | +if the single search would find nothing. |
| 1459 | +\end{note} |
| 1460 | + |
| 1461 | +\pnum |
| 1462 | +The \defn{lookup set} for $N$ in $C$, called $S(N,C)$, |
| 1463 | +consists of two component sets: |
| 1464 | +the \term{declaration set}, a set of members named $N$; and |
| 1465 | +the \term{subobject set}, |
| 1466 | +a set of subobjects where declarations of these members were found |
| 1467 | +(possibly via \grammarterm{using-declaration}{s}). |
| 1468 | +In the declaration set, type declarations (including injected-class-names) |
| 1469 | +are replaced by the types they designate. $S(N,C)$ is calculated as follows: |
| 1470 | + |
| 1471 | +\pnum |
| 1472 | +The declaration set is the result of |
| 1473 | +a single search in the scope of $C$ for $N$ |
| 1474 | +from immediately after the \grammarterm{class-specifier} of $C$ |
| 1475 | +if $P$ is in a complete-class context of $C$ or |
| 1476 | +from $P$ otherwise. |
| 1477 | +If the resulting declaration set is not empty, the subobject set |
| 1478 | +contains $C$ itself, and calculation is complete. |
| 1479 | + |
| 1480 | +\pnum |
| 1481 | +Otherwise (i.e., $C$ does not contain a declaration of $N$ |
| 1482 | +or the resulting declaration set is empty), $S(N,C)$ is initially empty. |
| 1483 | +Calculate the lookup set for $N$ |
| 1484 | +in each direct non-dependent\iref{temp.dep.type} base class subobject $B_i$, and |
| 1485 | +merge each such lookup set $S(N,B_i)$ in turn into $S(N,C)$. |
| 1486 | +\begin{note} |
| 1487 | +If $T$ is incomplete, |
| 1488 | +only base classes whose \grammarterm{base-specifier} appears before $P$ |
| 1489 | +are considered. |
| 1490 | +If $T$ is an instantiated class, its base classes are not dependent. |
| 1491 | +\end{note} |
| 1492 | + |
| 1493 | +\pnum |
| 1494 | +The following steps define the result of merging lookup set $S(N,B_i)$ |
| 1495 | +into the intermediate $S(N,C)$: |
| 1496 | + |
| 1497 | +\begin{itemize} |
| 1498 | +\item If each of the subobject members of $S(N,B_i)$ is a base class |
| 1499 | +subobject of at least one of the subobject members of $S(N,C)$, or if |
| 1500 | +$S(N,B_i)$ is empty, $S(N,C)$ is unchanged and the merge is complete. |
| 1501 | +Conversely, if each of the subobject members of $S(N,C)$ is a base class |
| 1502 | +subobject of at least one of the subobject members of $S(N,B_i)$, or if |
| 1503 | +$S(N,C)$ is empty, the new $S(N,C)$ is a copy of $S(N,B_i)$. |
| 1504 | + |
| 1505 | +\item Otherwise, if the declaration sets of $S(N,B_i)$ and $S(N,C)$ |
| 1506 | +differ, the merge is ambiguous: the new $S(N,C)$ is a lookup set with an |
| 1507 | +invalid declaration set and the union of the subobject sets. In |
| 1508 | +subsequent merges, an invalid declaration set is considered different |
| 1509 | +from any other. |
| 1510 | + |
| 1511 | +\item Otherwise, the new $S(N,C)$ is a lookup set with the shared set of |
| 1512 | +declarations and the union of the subobject sets. |
| 1513 | +\end{itemize} |
| 1514 | + |
| 1515 | +\pnum |
| 1516 | +The result of the search is the declaration set of $S(N,T)$. |
| 1517 | +If it is an invalid set, the program is ill-formed. |
| 1518 | +If it differs from the result of a search in $T$ for $N$ |
| 1519 | +from immediately after the \grammarterm{class-specifier} of $T$, |
| 1520 | +the program is ill-formed, no diagnostic required. |
| 1521 | +\begin{example} |
| 1522 | +\begin{codeblock} |
| 1523 | +struct A { int x; }; // S(x,A) = \{ \{ \tcode{A::x} \}, \{ \tcode{A} \} \} |
| 1524 | +struct B { float x; }; // S(x,B) = \{ \{ \tcode{B::x} \}, \{ \tcode{B} \} \} |
| 1525 | +struct C: public A, public B { }; // S(x,C) = \{ invalid, \{ \tcode{A} in \tcode{C}, \tcode{B} in \tcode{C} \} \} |
| 1526 | +struct D: public virtual C { }; // S(x,D) = S(x,C) |
| 1527 | +struct E: public virtual C { char x; }; // S(x,E) = \{ \{ \tcode{E::x} \}, \{ \tcode{E} \} \} |
| 1528 | +struct F: public D, public E { }; // S(x,F) = S(x,E) |
| 1529 | +int main() { |
| 1530 | + F f; |
| 1531 | + f.x = 0; // OK, lookup finds \tcode{E::x} |
| 1532 | +} |
| 1533 | +\end{codeblock} |
| 1534 | + |
| 1535 | +$S(\tcode{x},\tcode{F})$ is unambiguous because the \tcode{A} and \tcode{B} base |
| 1536 | +class subobjects of \tcode{D} are also base class subobjects of \tcode{E}, so |
| 1537 | +$S(\tcode{x},\tcode{D})$ is discarded in the first merge step. |
| 1538 | +\end{example} |
| 1539 | + |
| 1540 | +\pnum |
| 1541 | +If $N$ is a non-dependent \grammarterm{conversion-function-id}, |
| 1542 | +conversion function templates that are members of $T$ are considered. |
| 1543 | +For each such template $F$, the lookup set $S(t,T)$ is constructed, |
| 1544 | +considering a function template declaration to have the name $t$ |
| 1545 | +only if it corresponds to a declaration of $F$\iref{basic.scope.scope}. |
| 1546 | +The members of the declaration set of each such lookup set, |
| 1547 | +which shall not be an invalid set, are included in the result. |
| 1548 | +\begin{note} |
| 1549 | +Overload resolution will discard those |
| 1550 | +that cannot convert to the type specified by $N$\iref{temp.over}. |
| 1551 | +\end{note} |
| 1552 | + |
| 1553 | +\pnum |
| 1554 | +\begin{note} |
| 1555 | +A static member, a nested type or an enumerator defined in a base class |
| 1556 | +\tcode{T} can unambiguously be found even if an object has more than one |
| 1557 | +base class subobject of type \tcode{T}. Two base class subobjects share |
| 1558 | +the non-static member subobjects of their common virtual base classes. |
| 1559 | +\end{note} |
| 1560 | +\begin{example} |
| 1561 | +\begin{codeblock} |
| 1562 | +struct V { |
| 1563 | + int v; |
| 1564 | +}; |
| 1565 | +struct A { |
| 1566 | + int a; |
| 1567 | + static int s; |
| 1568 | + enum { e }; |
| 1569 | +}; |
| 1570 | +struct B : A, virtual V { }; |
| 1571 | +struct C : A, virtual V { }; |
| 1572 | +struct D : B, C { }; |
| 1573 | + |
| 1574 | +void f(D* pd) { |
| 1575 | + pd->v++; // OK: only one \tcode{v} (virtual) |
| 1576 | + pd->s++; // OK: only one \tcode{s} (static) |
| 1577 | + int i = pd->e; // OK: only one \tcode{e} (enumerator) |
| 1578 | + pd->a++; // error: ambiguous: two \tcode{a}{s} in \tcode{D} |
| 1579 | +} |
| 1580 | +\end{codeblock} |
| 1581 | +\end{example} |
| 1582 | + |
| 1583 | +\pnum |
| 1584 | +\begin{note} |
| 1585 | +\indextext{dominance!virtual base class}% |
| 1586 | +When virtual base classes are used, a hidden declaration can be reached |
| 1587 | +along a path through the subobject lattice that does not pass through |
| 1588 | +the hiding declaration. This is not an ambiguity. The identical use with |
| 1589 | +non-virtual base classes is an ambiguity; in that case there is no |
| 1590 | +unique instance of the name that hides all the others. |
| 1591 | +\end{note} |
| 1592 | +\begin{example} |
| 1593 | +\begin{codeblock} |
| 1594 | +struct V { int f(); int x; }; |
| 1595 | +struct W { int g(); int y; }; |
| 1596 | +struct B : virtual V, W { |
| 1597 | + int f(); int x; |
| 1598 | + int g(); int y; |
| 1599 | +}; |
| 1600 | +struct C : virtual V, W { }; |
| 1601 | + |
| 1602 | +struct D : B, C { void glorp(); }; |
| 1603 | +\end{codeblock} |
| 1604 | + |
| 1605 | +\begin{importgraphic} |
| 1606 | +{Name lookup} |
| 1607 | +{class.lookup} |
| 1608 | +{figname.pdf} |
| 1609 | +\end{importgraphic} |
| 1610 | + |
| 1611 | +As illustrated in \fref{class.lookup}, |
| 1612 | +the names declared in \tcode{V} and the left-hand instance of \tcode{W} |
| 1613 | +are hidden by those in \tcode{B}, but the names declared in the |
| 1614 | +right-hand instance of \tcode{W} are not hidden at all. |
| 1615 | +\begin{codeblock} |
| 1616 | +void D::glorp() { |
| 1617 | + x++; // OK: \tcode{B::x} hides \tcode{V::x} |
| 1618 | + f(); // OK: \tcode{B::f()} hides \tcode{V::f()} |
| 1619 | + y++; // error: \tcode{B::y} and \tcode{C}'s \tcode{W::y} |
| 1620 | + g(); // error: \tcode{B::g()} and \tcode{C}'s \tcode{W::g()} |
| 1621 | +} |
| 1622 | +\end{codeblock} |
| 1623 | +\end{example} |
| 1624 | +\indextext{ambiguity!class conversion}% |
| 1625 | + |
| 1626 | +\pnum |
| 1627 | +An explicit or implicit conversion from a pointer to or |
| 1628 | +an expression designating an object |
| 1629 | +of a |
| 1630 | +derived class to a pointer or reference to one of its base classes shall |
| 1631 | +unambiguously refer to a unique object representing the base class. |
| 1632 | +\begin{example} |
| 1633 | +\begin{codeblock} |
| 1634 | +struct V { }; |
| 1635 | +struct A { }; |
| 1636 | +struct B : A, virtual V { }; |
| 1637 | +struct C : A, virtual V { }; |
| 1638 | +struct D : B, C { }; |
| 1639 | + |
| 1640 | +void g() { |
| 1641 | + D d; |
| 1642 | + B* pb = &d; |
| 1643 | + A* pa = &d; // error: ambiguous: \tcode{C}'s \tcode{A} or \tcode{B}'s \tcode{A}? |
| 1644 | + V* pv = &d; // OK: only one \tcode{V} subobject |
| 1645 | +} |
| 1646 | +\end{codeblock} |
| 1647 | +\end{example} |
| 1648 | + |
| 1649 | +\pnum |
| 1650 | +\begin{note} |
| 1651 | +Even if the result of name lookup is unambiguous, use of a name found in |
| 1652 | +multiple subobjects might still be |
| 1653 | +ambiguous~(\ref{conv.mem}, \ref{expr.ref}, \ref{class.access.base}). |
| 1654 | +\end{note} |
| 1655 | +\begin{example} |
| 1656 | +\begin{codeblock} |
| 1657 | +struct B1 { |
| 1658 | + void f(); |
| 1659 | + static void f(int); |
| 1660 | + int i; |
| 1661 | +}; |
| 1662 | +struct B2 { |
| 1663 | + void f(double); |
| 1664 | +}; |
| 1665 | +struct I1: B1 { }; |
| 1666 | +struct I2: B1 { }; |
| 1667 | + |
| 1668 | +struct D: I1, I2, B2 { |
| 1669 | + using B1::f; |
| 1670 | + using B2::f; |
| 1671 | + void g() { |
| 1672 | + f(); // Ambiguous conversion of \tcode{this} |
| 1673 | + f(0); // Unambiguous (static) |
| 1674 | + f(0.0); // Unambiguous (only one \tcode{B2}) |
| 1675 | + int B1::* mpB1 = &D::i; // Unambiguous |
| 1676 | + int D::* mpD = &D::i; // Ambiguous conversion |
| 1677 | + } |
| 1678 | +}; |
| 1679 | +\end{codeblock} |
| 1680 | +\end{example} |
| 1681 | + |
1445 | 1682 | \rSec2[basic.lookup.unqual]{Unqualified name lookup}
|
1446 | 1683 |
|
1447 | 1684 | \pnum
|
|
1485 | 1722 | template <class T> int h(T);
|
1486 | 1723 | }
|
1487 | 1724 |
|
1488 |
| -int x = f<N::A>(N::A()); // OK: lookup of \tcode{f} finds nothing, \tcode{f} treated as template name |
| 1725 | +int x = f<N::A>(N::A()); // OK: lookup of \tcode{f} finds nothing, $N$ treated as template name |
1489 | 1726 | int y = g<N::A>(N::A()); // OK: lookup of \tcode{g} finds a function, \tcode{g} treated as template name
|
1490 | 1727 | int z = h<N::A>(N::A()); // error: \tcode{h<} does not begin a \grammarterm{template-id}
|
1491 | 1728 | \end{codeblock}
|
|
1499 | 1736 | friend void f(A &);
|
1500 | 1737 | operator int();
|
1501 | 1738 | void g(A a) {
|
1502 |
| - int i = f(a); // \tcode{f} is the typedef, not the friend function: equivalent to \tcode{int(a)} |
| 1739 | + int i = f(a); // $N$ is the typedef, not the friend function: equivalent to \tcode{int(a)} |
1503 | 1740 | }
|
1504 | 1741 | };
|
1505 | 1742 | }
|
|
0 commit comments