https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109918

            Bug ID: 109918
           Summary: Unexpected -Woverloaded-virtual with virtual
                    conversion operators
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: romain.geissler at amadeus dot com
  Target Milestone: ---

Hi,

Now that the -Woverloaded-virtual=1 is enabled by default with -Wall, the
following code raises warning while I think it should not (for the specific
case of conversion operators):

#include <iostream>

struct A
{
    virtual operator int() { return 42; }

    virtual operator char() = 0;
};

struct B : public A
{
    operator char() override { return 'A'; }
};

int main()
{
    B b;

    std::cout << static_cast<int>(b) << std::endl; // int conversion was not
hiden, contrary to what -Woverloaded-virtual claims
    std::cout << static_cast<char>(b) << std::endl;
}


Godbolt link: https://godbolt.org/z/4Wb9rxbMP

Compiled with -Wall, it raises this warning:

<source>:5:13: warning: 'virtual A::operator int()' was hidden
[-Woverloaded-virtual=]
    5 |     virtual operator int() { return 42; }
      |             ^~~~~~~~
<built-in>: note:   by 'operator'
ASM generation compiler returned: 0
<source>:5:13: warning: 'virtual A::operator int()' was hidden
[-Woverloaded-virtual=]
    5 |     virtual operator int() { return 42; }
      |             ^~~~~~~~
<built-in>: note:   by 'operator'
Execution build compiler returned: 0
Program returned: 0
42
A


I have hit this issue in a real code base, while migrating to gcc 13.

Cheers,
Romain
  • [Bug c++/109918] New: Unex... romain.geissler at amadeus dot com via Gcc-bugs

Reply via email to