Title
Overriding virtual functions, also with explicit object parameters
Status
open
Section
11.7.3 [class.virtual]
Submitter
Jens Maurer

Created on 2021-12-10.00:00:00 last changed 2 months ago

Messages

Date: 2022-03-24.07:15:00

Suggested resolution:

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 considered as a member of D (12.2.2.1 [over.match.funcs.general]), ignoring trailing requires-clauses, and, if both are implicit object member functions, they have the same ref-qualifier (or absence thereof), and, if at least one is an explicit object member function, ignoring object parameters, then G overrides [ Footnote: ... ] F .
Date: 2021-12-10.00:00:00

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
  • ...
  • each declares a function or function template, except when
    • both declare functions with the same non-object-parameter-type-list, equivalent (13.7.7.2 [temp.over.link]) trailing requires-clauses (if any, except as specified in 13.7.5 [temp.friend]), and, if both are non-static members, they have corresponding object parameters, or
    • both declare function templates with...

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:
  • exactly one is an implicit object member function with no ref-qualifier and the types of their object parameters (9.3.4.6 [dcl.fct]), after removing top-level references, are the same, or
  • their object parameters have the same type.

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.

History
Date User Action Args
2022-03-24 07:15:00adminsetmessages: + msg6772
2021-12-10 00:00:00admincreate