[Bug c++/93984] New: spurious Wclass-conversion warning

stsp at users dot sourceforge.net gcc-bugzilla@gcc.gnu.org
Sat Feb 29 23:02:00 GMT 2020


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

            Bug ID: 93984
           Summary: spurious Wclass-conversion warning
           Product: gcc
           Version: 9.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: stsp at users dot sourceforge.net
  Target Milestone: ---

#include <iostream>

struct D;
struct B {
    virtual operator D() = 0;
};
struct D : B
{
    operator D() override { std::cout << "conv" << std::endl; return D(); }
};
.
int main()
{
    D obj;
    B& br = obj;
    (D)br; // calls D::operator D() through virtual dispatch
    return 0;
}

$ LC_ALL=C g++ -Wall -o vconv vconv.cpp 
vconv.cpp:9:5: warning: converting 'D' to the same type will never use a type
conversion operator [-Wclass-conversion]
    9 |     operator D() override { std::cout << "conv" << std::endl; return
D(); }
      |     ^~~~~~~~


$ ./vconv 
conv


The example above shows that the warning
is spurious. Converting to the same type
will indeed never use the conversion
operator. But the above case converts
from B to D, so the warning does not apply.
It may be quite difficult to check properly,
but in this particular example the "override"
keyword is a clear hint that it is going
to be used via the virtual dispatch.


More information about the Gcc-bugs mailing list