c++/10784: [diagnostic] Warning about choosing custom operator over copy constructor cannot be turned off (and it's useless in the first place)

giovannibajo@libero.it giovannibajo@libero.it
Wed May 14 16:24:00 GMT 2003


>Number:         10784
>Category:       c++
>Synopsis:       [diagnostic] Warning about choosing custom operator over copy constructor cannot be turned off (and it's useless in the first place)
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed May 14 16:16:00 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     Giovanni Bajo
>Release:        3.0 -> 3.4
>Organization:
>Environment:
i686-pc-cygwin
>Description:
This was originally c++/4337, but I opened a new PR to make
it clearer.

--------------------------------
#include <stdio.h>

struct A {
};
struct A1 : public A {

    A1(const A &) {
        printf("A1(const A &)\n");
    }
    A1() {
        printf("A1()\n");
    }
};
A1 X;
struct A2 : public A {
    operator A1() {
        printf("operator A1()\n");
        return X;
    }
};

int main()
{
    A2 a2;
    A1 a1;
    a1 = a2;
}
--------------------------------
pr4337.cpp: In function `int main()':
pr4337.cpp:26: warning: choosing `A2::operator A1()' over `A1::A1(const A&)'
pr4337.cpp:26: warning:   for conversion from `A2' to `A1'
pr4337.cpp:26: warning:   because conversion sequence for the argument is better

GCC has always emitted the previous warning since at least
2.95. There is no (known?) way to turn it off, and it
breaks compilation. Besides, the code is not ambigous,
I think that the compiler should always select the custom
conversion operator, so there is nothing to warn about.
The output is:

A1()
A1()
operator A1()

and every compiler I have gives the same output.

Such a warning should be moved to -Wextra, if kept at all.
>How-To-Repeat:
Compile the above snippet.
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-bugs mailing list