Topic: Multiple user-defined conversions


Author: stephen.clamage@sun.com (Steve Clamage)
Date: Sat, 7 Aug 2004 20:50:57 GMT
Raw View
Stephan Bergmann wrote:
> Hi all,
>
> Is the following code correct?
>
>   struct C1 { C1(C1 const &); };
>   struct C2 { operator C1 &(); };
>   C1 f(C2 c2) { return c2; }
>
> All but one compilers I checked accept it without any diagnostic, and
> one generated a warning that more than one user-defined conversions has
> been implicitly applied.

I don't think your example illustrates your question. There is only
one user-defined conversion operator or constructor in the example --
C2::operator C1&() -- and only that one conversion is needed in
function f().

---
Steve Clamage, stephen.clamage@sun.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: stephan.bergmann@sun.com (Stephan Bergmann)
Date: Mon, 9 Aug 2004 15:34:20 GMT
Raw View
Tao Lu wrote:
> Hi Stephan,
>
> "Stephan Bergmann" <stephan.bergmann@sun.com> wrote in message
> news:cevfg7$kg7$1@new-usenet.uk.sun.com...
>
>>Hi all,
>>
>>Is the following code correct?
>>
>>   struct C1 { C1(C1 const &); };
>>   struct C2 { operator C1 &(); };
>>   C1 f(C2 c2) { return c2; }
>>
>>All but one compilers I checked accept it without any diagnostic, and
>>one generated a warning that more than one user-defined conversions has
>>been implicitly applied.
>>
>>However, looking into the Standard, I think the code is incorrect (and
>>should be rejected by any compiler):
>>
>>12.3/1:  "Type conversions of class objects can be specified by
>>constructors and by conversion functions.  These conversions are called
>>user-defined conversions and are used for implicit type conversions [...]"
>>
>>12.3.1/3:  "A copy-constructor (12.8) is a converting constructor."
>>
>>12.3/4:  "At most one user-defined conversion (constructor or conversion
>>function) is implicitly applied to a single value."
>
>
> The question here is how many "values" are involved during the process.
> The implementation of the conversion operator of C2 could return a totally
> different entity from c2 or run a specific cast on it before returning it.
> So the compiler only runs 1 user defined conversion implicitly on a single
> value.

Then, how would an (illegal) conversion sequence look like that applied
multiple user-defined conversions to a single class object value?  Isn't
the whole idea of a conversion to translate from one "value" to another
one?  The more I think about it, the more I'm puzzled what the exact
meaning of the term "value" shall be in 12.3/4.

-Stephan

> HTH,
>
> -Tao

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: stephan.bergmann@sun.com (Stephan Bergmann)
Date: Mon, 9 Aug 2004 16:42:44 GMT
Raw View
Steve Clamage wrote:
> Stephan Bergmann wrote:
>
>> Hi all,
>>
>> Is the following code correct?
>>
>>   struct C1 { C1(C1 const &); };
>>   struct C2 { operator C1 &(); };
>>   C1 f(C2 c2) { return c2; }
>>
>> All but one compilers I checked accept it without any diagnostic, and
>> one generated a warning that more than one user-defined conversions
>> has been implicitly applied.
>
>
> I don't think your example illustrates your question. There is only one
> user-defined conversion operator or constructor in the example --
> C2::operator C1&() -- and only that one conversion is needed in function
> f().

My understanding was that to go from C2 to C1, first the user-defined
conversion function C2::operator C1 &() is called, and then the
user-defined converting constructor (which happens to also be a copy
constructor) C1::C1(C1 const &).

-Stephan

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: lu@dre.vanderbilt.edu ("Tao Lu")
Date: Mon, 9 Aug 2004 16:42:40 GMT
Raw View
Hi Steve,
> >
> > Is the following code correct?
> >
> >   struct C1 { C1(C1 const &); };
> >   struct C2 { operator C1 &(); };
> >   C1 f(C2 c2) { return c2; }
> >
> > All but one compilers I checked accept it without any diagnostic, and
> > one generated a warning that more than one user-defined conversions has
> > been implicitly applied.
>
> I don't think your example illustrates your question. There is only
> one user-defined conversion operator or constructor in the example --
> C2::operator C1&() -- and only that one conversion is needed in
> function f().

The copy constructor is also needed.

-Tao


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: stephan.bergmann@sun.com (Stephan Bergmann)
Date: Fri, 6 Aug 2004 15:22:08 GMT
Raw View
Hi all,

Is the following code correct?

   struct C1 { C1(C1 const &); };
   struct C2 { operator C1 &(); };
   C1 f(C2 c2) { return c2; }

All but one compilers I checked accept it without any diagnostic, and
one generated a warning that more than one user-defined conversions has
been implicitly applied.

However, looking into the Standard, I think the code is incorrect (and
should be rejected by any compiler):

12.3/1:  "Type conversions of class objects can be specified by
constructors and by conversion functions.  These conversions are called
user-defined conversions and are used for implicit type conversions [...]"

12.3.1/3:  "A copy-constructor (12.8) is a converting constructor."

12.3/4:  "At most one user-defined conversion (constructor or conversion
function) is implicitly applied to a single value."

Am I missing anything?
-Stephan

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: lu@dre.vanderbilt.edu ("Tao Lu")
Date: Fri, 6 Aug 2004 20:04:03 GMT
Raw View
Hi Stephan,

"Stephan Bergmann" <stephan.bergmann@sun.com> wrote in message
news:cevfg7$kg7$1@new-usenet.uk.sun.com...
> Hi all,
>
> Is the following code correct?
>
>    struct C1 { C1(C1 const &); };
>    struct C2 { operator C1 &(); };
>    C1 f(C2 c2) { return c2; }
>
> All but one compilers I checked accept it without any diagnostic, and
> one generated a warning that more than one user-defined conversions has
> been implicitly applied.
>
> However, looking into the Standard, I think the code is incorrect (and
> should be rejected by any compiler):
>
> 12.3/1:  "Type conversions of class objects can be specified by
> constructors and by conversion functions.  These conversions are called
> user-defined conversions and are used for implicit type conversions [...]"
>
> 12.3.1/3:  "A copy-constructor (12.8) is a converting constructor."
>
> 12.3/4:  "At most one user-defined conversion (constructor or conversion
> function) is implicitly applied to a single value."

The question here is how many "values" are involved during the process.
The implementation of the conversion operator of C2 could return a totally
different entity from c2 or run a specific cast on it before returning it.
So the compiler only runs 1 user defined conversion implicitly on a single
value.

HTH,

-Tao


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]