https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66615
Bug ID: 66615
Summary: Ambiguous conversion operators, explicit unambiguous
conversion operator and copy initialization
Product: gcc
Version: 5.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: fynjycfdby at gmail dot com
Target Milestone: ---
The following code is wrongly accepted, the output is an arbitrary number:
-
#include
struct A {
explicit operator int() {
return 42;
}
operator float() {
return 123.0f;
}
operator double() {
return 456.0;
}
};
int main() {
int i = A(); // note copy-initialization
std::cout << i << std::endl;
}
-
With -Wall it generates a warning:
main.cpp: In function 'int main()':
main.cpp:19:18: warning: 'i' is used uninitialized in this function
[-Wuninitialized]
std::cout << i << std::endl;
The correct behavior should be failure to compile, because the operator int()
is explicit, and other implicit conversion operators make conversion to int
ambiguous.
I'm using MacPorts g++-mp-5
$ g++-mp-5 -v
Using built-in specs.
COLLECT_GCC=g++-mp-5
COLLECT_LTO_WRAPPER=/opt/local/libexec/gcc/x86_64-apple-darwin14/5.1.0/lto-wrapper
Target: x86_64-apple-darwin14
Configured with:
/opt/local/var/macports/build/_opt_mports_dports_lang_gcc5/gcc5/work/gcc-5.1.0/configure
--prefix=/opt/local --build=x86_64-apple-darwin14
--enable-languages=c,c++,objc,obj-c++,lto,fortran,java
--libdir=/opt/local/lib/gcc5 --includedir=/opt/local/include/gcc5
--infodir=/opt/local/share/info --mandir=/opt/local/share/man
--datarootdir=/opt/local/share/gcc-5 --with-local-prefix=/opt/local
--with-system-zlib --disable-nls --program-suffix=-mp-5
--with-gxx-include-dir=/opt/local/include/gcc5/c++/ --with-gmp=/opt/local
--with-mpfr=/opt/local --with-mpc=/opt/local --with-isl=/opt/local
--enable-stage1-checking --disable-multilib --enable-lto
--enable-libstdcxx-time --with-as=/opt/local/bin/as --with-ld=/opt/local/bin/ld
--with-ar=/opt/local/bin/ar --with-bugurl=https://trac.macports.org/newticket
--with-pkgversion='MacPorts gcc5 5.1.0_1'
Thread model: posix
gcc version 5.1.0 (MacPorts gcc5 5.1.0_1)