Compiler chooses conv ctor - why?
- From: "Cezary H. Noweta" <chncc@xxxxxxxxxxxxxxx>
- Date: Sun, 13 Apr 2008 01:00:40 +0200
Hello,
In the following code conversion from class A object to class B object can be done in two ways: (1) by conversion function being member of class A (line 6); (2) by conversion constructor being member of class B (line 11). However conversion constructor is called. The sole way to make A::operator B() to be called is removing ,,const'' qualifiers (at lines 11 & 25). This causes that the only implicit conversion sequence goes throughout conversion function (declared at line 6). Why? It seems that according to 13.3 of ISO 14882 none of the sequences is better then other. Thus ambiguity should occur causing error. Could somebody point out what part of ISO 14882 causes that conversion constructor (and not conv foo) should be chosen and why conv ctor is better then conv foo?
-- best regards
Cezary Noweta
======
#include <stdio.h>
struct B;
struct A {
operator const B() const; // Line 6
};
struct B {
B();
B(const A& a); // Line 11
const B& operator=(const B&) const;
};
A::operator const B() const
{
printf("conv foo called\n");
return(B());
}
B::B()
{
}
B::B(const A& a) // Line 25
{
printf("conv ctor called\n");
}
const B& B::operator=(const B&) const
{
return(this[0]);
}
const B foo(const A a)
{
return(a);
}
const B foo1(const A a)
{
return((const B)a);
}
const B foo2(const A a)
{
return(const B(a));
}
main()
{
const A a;
const B b;
b = foo(a);
b = foo1(a);
b = foo2(a);
}
======
.
- Follow-Ups:
- Re: Compiler chooses conv ctor - why?
- From: David Lowndes
- Re: Compiler chooses conv ctor - why?
- From: Alex Blekhman
- Re: Compiler chooses conv ctor - why?
- Prev by Date: Re: Standard C Library and utf-8
- Next by Date: Re: C function prototype
- Previous by thread: which design is better to wrap another class instance
- Next by thread: Re: Compiler chooses conv ctor - why?
- Index(es):
Relevant Pages
|