This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Implicit conversion error using g++
- To: gcc-help at gcc dot gnu dot org
- Subject: Implicit conversion error using g++
- From: Daniel dot Walker at lhsl dot com
- Date: Mon, 9 Jul 2001 11:21:58 -0700
I have a question about when g++ accepts implicit conversions using user
defined types and when it doesn't. Consider:
class Foo { };
class Bar {
public: operator Foo() { return Foo(); }
};
class Baz {
public: Baz(const Foo & f) { }
};
int main(int argc, char ** argv) {
Bar bar;
Baz b1(bar); // line 13
Baz b2 = bar; // line 14, error
}
I get the following error with gcc v3 (prerelease) 20010514:
g++ foo.cpp
foo.cpp: In function `int main(int, char**)':
foo.cpp:14: conversion from `Bar' to non-scalar type `Baz' requested
Why are lines 13 and 14 not equivalent? Initializing b1 uses Bar's cast
operator implicitly but initializing b2 doesn't. Could someone please
explain what g++'s policy is and also what does ISO say about user defined
casts during initialization? I appreciate it!
Daniel Walker