bogus error on constructor call with gcc 2.95.2
E. Jay Berkenbilt
ejb@ql.org
Sun Oct 15 20:12:00 GMT 2000
With gcc 2.95.2, on this C++ file:
---------------------------------------------------------------------------
class A
{
public:
A(int, int);
};
class B
{
public:
B(A const&);
};
void f()
{
B(A(0, 1));
}
---------------------------------------------------------------------------
you get this output:
---------------------------------------------------------------------------
a.cc: In function `void f()':
a.cc:15: no matching function for call to `B::B (int, int)'
a.cc:10: candidates are: B::B(const A &)
a.cc:11: B::B(const B &)
---------------------------------------------------------------------------
NOTES:
* I believe that my code above is valid. An expression is a valid
statement in C++, and B(A(0, 1)) is certainly a valid expression:
it creates a temporary B initialized by a temporary A. The above
code should simply call the A constructor, the B constructor, the B
destructor, and the A destructor since the objects are not bound to
anything.
* Even if someone maintains that the above code is not valid, the
error message seems wrong. It is certainly the case that B is
being called with an A as an argument, not with A's arguments. I
have not tried declaring an (int, int) constructor to B, defining
all these, linking, and seeing which constructor is called.
* The following code fragments compile without error:
B b(A(0, 1));
A a(0, 1);
B(a);
* I've used this type of construct before including in older versions
of gcc. It may look odd at first, but it can be useful to test
whether creation of an object will succeed. Mainly I use
constructs like this in test suites to ensure that constructing an
object with invalid arguments will raise a suitable exception....
* This problem also exists in the fictitious 2.96 release included in
RedHat Linux 7.0. However, I tested the above in 2.95.2 before
reporting the bug. I have not tested it in a current developer
snapshot.
--
E. Jay Berkenbilt (ejb@ql.org) | http://www.ql.org/q/
More information about the Gcc-bugs
mailing list