This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 116a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-12-19


2554. Overriding virtual functions, also with explicit object parameters

Section: 11.7.3  [class.virtual]     Status: review     Submitter: Jens Maurer     Date: 2021-12-10

Consider:

  struct B {
    virtual void f();   // #1
  };

  struct D : B {
    void f();           // #2
  };

Subclause 11.7.3 [class.virtual] paragraph 2 says:

If a virtual member function F is declared in a class B, and, in a class D derived (directly or indirectly) from B, a declaration of a member function G corresponds (6.4.1 [basic.scope.scope]) to a declaration of F, ignoring trailing requires-clauses, then G overrides [ Footnote: ... ] F .

Subclause 6.4.1 [basic.scope.scope] paragraph 4 defines "corresponds" as follows:

Two declarations correspond if they (re)introduce the same name, both declare constructors, or both declare destructors, unless

Subclause 6.4.1 [basic.scope.scope] paragraph 3 defines "corresponding object parameters" as follows:

Two non-static member functions have corresponding object parameters if:

In the example, B::f has an object parameter of type B, but D::f has an object parameter of type D. Thus, the two functions do not correspond, and thus D::f does not override B::f. That is an unintended alteration of the status quo ante.

See also issue 2553.

Proposed resolution:

  1. Change in 11.7.3 [class.virtual] paragraph 2 as follows:

    If a virtual member function F is declared in a class B, and, in a class D derived (directly or indirectly) from B, a declaration of a member function G corresponds (6.4.1 [basic.scope.scope]) to a declaration of F as if declared in D (12.2.2.1 [over.match.funcs.general]), ignoring trailing requires-clauses, and, if G is an explicit object member function, ignoring object parameters, and, if G is an implicit object member function, F and G have the same ref-qualifier (or absence thereof), then G overrides [ Footnote: ... ] F .
  2. Remove 11.7.3 [class.virtual] paragraph 7 as follows:

    The ref-qualifier , or lack thereof, of an overriding function shall be the same as that of the overridden function.