Implicit conversion error using g++

Daniel.Walker@lhsl.com Daniel.Walker@lhsl.com
Mon Jul 9 11:25:00 GMT 2001


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



More information about the Gcc-help mailing list