Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[class.virtual] Corresponding declaration vs. overriding a virtual function CWG2496 #4699

Closed
xmh0511 opened this issue Jun 16, 2021 · 3 comments · Fixed by #5030
Closed

[class.virtual] Corresponding declaration vs. overriding a virtual function CWG2496 #4699

xmh0511 opened this issue Jun 16, 2021 · 3 comments · Fixed by #5030
Labels
cwg Issue must be reviewed by CWG. not-editorial Issue is not deemed editorial; the editorial issue is kept open for tracking.

Comments

@xmh0511
Copy link
Contributor

xmh0511 commented Jun 16, 2021

Please see this example

#include <iostream>
class B{
public:
    virtual void fun() &{  //#1
        std::cout<<"base\n";
    }
};
class A:public B{
public:
  void fun(){  //#2
      std::cout<<"override\n";
  }
};
int main(){
   A  a;
   B* ptr = &a;
   ptr->fun();
}

GCC prints base while Clang prompts an error diagnosis. In the current draft, the relevant rule is defined like that:

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 ([basic.scope.scope]) to a declaration of F, ignoring trailing requires-clauses, then G overrides F.

Simultaneously, the rule for defining two declarations who correspond is defined like that:

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 parameter-type-list, equivalent ([temp.over.link]) trailing requires-clauses (if any, except as specified in [temp.friend]), and, if both are non-static members, the same cv-qualifiers (if any) and ref-qualifier (if both have one), or

In this example, #1 and #2 have the same parameter-type-list, #1 has one ref-qualifier while #2 does not have, hence the latter restriction could be ignored, hence, for these two declarations, we could say they correspond. Hence, #2 overrides #1 that is declared as a virtual function. Hence, we could say both GCC and Clang have the wrong result.

However, in the c++20 standard, the restriction for how to determine a declaration declared in the derived class overrides the virtual function declared in the base class is:

If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name, parameter-type-list ([dcl.fct]), cv-qualification, and ref-qualifier (or absence of same) as Base​::​vf is declared, then Derived​::​vf overrides Base​::​vf.

It's obvious that the current rule has changed its original meaning. Is it a defect in the new rule? Or, it's just the artificial design?

@jensmaurer
Copy link
Member

@opensdh, that looks like a bug introduced by P1787R6.

I think we want to be able to overload & and && functions independently (similar to const and non-const), thus we need a sentence in [class.virtual] that if an overrider has no ref-qualifier, the overridden function must not have a ref-qualifier, either.

This smells like CWG material.

@jensmaurer jensmaurer added the cwg Issue must be reviewed by CWG. label Jun 16, 2021
@jensmaurer jensmaurer changed the title Just a corresponding declaration can override the virtual function, which has changed the original semantic [class.virtual] Corresponding declaration vs. overriding a virtual function Jun 16, 2021
@xmh0511
Copy link
Contributor Author

xmh0511 commented Aug 12, 2021

@jensmaurer This issue should be CWG2496

@jensmaurer jensmaurer changed the title [class.virtual] Corresponding declaration vs. overriding a virtual function [class.virtual] Corresponding declaration vs. overriding a virtual function CWG2496 Aug 12, 2021
@jensmaurer jensmaurer added the not-editorial Issue is not deemed editorial; the editorial issue is kept open for tracking. label Aug 12, 2021
@opensdh
Copy link
Contributor

opensdh commented Mar 24, 2022

The Core issue has been resolved, so this should presumably be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cwg Issue must be reviewed by CWG. not-editorial Issue is not deemed editorial; the editorial issue is kept open for tracking.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants