Re: Compiler chooses conv ctor - why?
- From: "Cezary H. Noweta" <chncc@xxxxxxxxxxxxxxx>
- Date: Mon, 14 Apr 2008 11:39:50 +0200
Hello,
Charles Wang[MSFT] wrote:
Could you please let me know why you think that this is a compiler bug? I think that this is just a way of compiler implementing conversion. In VC++, by default "B b=a" will call B's constructor "B(const A& a);" to perform the conversion; you can use "explicit" keyword before "B(const A& a);" to avoid such automatic constructor conversion, and then the conversion would happen on "A::operator const B() const".
Charles, thank you for the MSDN link. I've looked for an explanation of
such behaviour. Here it is. However, previously I've looked into ISO
14822 and have found no explanation. Those web pages have been finishing
my doubts.
I'm not very familiar with this ISO, as I've recently returned to C++
after a long period of time. However, I'll try to answer your question
and point out why this could be compiler bug. I'll refer to the original
ISO 14882 dated 2003-10-15, especially I'll not refer to the working
draft N2521 dated on February this year. Maybe something related
to this issue has changed after original ISO has been released, or maybe
I missed something. If somebody corrects me I'll be grateful.
1. By definition (8.5[12]) ,,B b=a;'' is copy-initialization (as
opposite to direct-initialization ,,B b(a);'' which drops away conv foos
and considers conv ctors only).
2. As class A and class B are not derived from each other, 8.5[14]
directs us to 13.3.1.4.
3. 13.3.1.4: candidate functions to invoke this task are conv ctor (
B::B(const A&); ), and conv foo ( A::operator B() const; ).
4. According to 13.3.3[1] we start at checking of parameter conversion
sequences, when selecting the best function. A parameter of conv ctor is
,,const A&'', and implicit object parameter of conv foo is ,,const A&''
too. Both of functions require the same qualification conversion, thus
both of them are indistinguishable from each other.
5. Next, according to the same 13.3.3[1], we land at the standard
conversion sequence of return types.
6. Both of foos return ,,B'', so no one is better then other, and
according to 13.3.3[2] the call (initialization) is ill-formed.
-- best regards
Cezary Noweta
.
- Follow-Ups:
- Re: Compiler chooses conv ctor - why?
- From: Charles Wang[MSFT]
- Re: Compiler chooses conv ctor - why?
- From: Alex Blekhman
- Re: Compiler chooses conv ctor - why?
- References:
- Compiler chooses conv ctor - why?
- From: Cezary H. Noweta
- Re: Compiler chooses conv ctor - why?
- From: Alex Blekhman
- Re: Compiler chooses conv ctor - why?
- From: Charles Wang[MSFT]
- Compiler chooses conv ctor - why?
- Prev by Date: Re: Compiler chooses conv ctor - why?
- Next by Date: Re: CString bug?
- Previous by thread: Re: Compiler chooses conv ctor - why?
- Next by thread: Re: Compiler chooses conv ctor - why?
- Index(es):
Relevant Pages
|
Loading