Topic: Pointer vs. Reference in user defined conversion


Author: "TiTi" <cenTipod@anTi.com>
Date: 2000/04/07
Raw View
Matthias Meixner <meixner@rbg.informatik.tu-darmstadt.de> wrote in message
news:8cfk5i$jqe$1@sun27.hrz.tu-darmstadt.de...
> once again I have a problem with user defined conversions and I am not
> sure, whether the problem lies within the standard or if it simply is
> a compiler bug.

> struct Z { Z(){} };
> struct A {
>    Z x;
>    operator Z *() { return &x; }
>    operator const Z *() { return &x; }
>    operator const Z *() const { return &x; }


Drop the first operator const Z*(). It should be a const operation
(logically) and it is giving you the ambiguous calls.


The same for your B class.


Problem is that you have two conversion operators for the same type (const
Z*). So it is not a compiler bug.



TiTi
--
comp.lang.c++
   * Read Shiva's "Welcome to comp.lang.c++" before posting to that
newsgroup:
   http://www.slack.net/~shiva/welcome.txt
   * FAQ (frequently asked questions) :
http://www.cerfnet.com/~mpcline/c++-faq-lite/
"Nothing is certain, and not even that" - Multatuli


---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: meixner@rbg.informatik.tu-darmstadt.de (Matthias Meixner)
Date: 2000/04/08
Raw View
TiTi (cenTipod@anTi.com) wrote:
: Matthias Meixner <meixner@rbg.informatik.tu-darmstadt.de> wrote in message
: news:8cfk5i$jqe$1@sun27.hrz.tu-darmstadt.de...
: > once again I have a problem with user defined conversions and I am not
: > sure, whether the problem lies within the standard or if it simply is
: > a compiler bug.
:
: > struct Z { Z(){} };
: > struct A {
: >    Z x;
: >    operator Z *() { return &x; }
: >    operator const Z *() { return &x; }
: >    operator const Z *() const { return &x; }
:
: Drop the first operator const Z*(). It should be a const operation
: (logically) and it is giving you the ambiguous calls.

No, the interesting point is, that for class A everything works fine,
the problem is just with class B.

In the meantime I was able to test another compiler (Sun Workshop) and
this compiler works fine, so I guess it is a bug in gcc.

- Matthias Meixner

--
Matthias Meixner                   meixner@rbg.informatik.tu-darmstadt.de
Technische Universit   t Darmstadt
Rechnerbetriebsgruppe                          Telefon (+49) 6151 16 6670
Wilhelminenstra   e 7, D-64283 Darmstadt, Germany    Fax (+49) 6151 16 4701






---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: meixner@rbg.informatik.tu-darmstadt.de (Matthias Meixner)
Date: 2000/04/08
Raw View
Since I had a chance to test my case with another compiler (Sun Workshop
Pro 5.0) that compiled without errors, I sent a bug report for gcc.
The answer was:

> Thanks for your bug report. This is not a bug in the compiler, but in
> your code. Both operators
>
>    operator Z &() { return x; }
>    operator const Z &() { return x; }
>
> are viable candidates, and they both have an identity conversion for
> the parameter. According to 13.3.3/1, the conversion sequences for the
> result are considered, which, again, are identity
> conversions. According to 13.3.3/2, your program is ill-formed.
>
> If you question this line of reasoning, please discuss it in one of
> the public C++ fora first, eg. comp.lang.c++.moderated, or
> comp.std.c++.

I want to opt for the last suggestion: :-)

I think the conversion sequences for the results are different:

      Z& -> const Z& (Qualification adjustment)
const Z& -> const Z& (Identity)

So the second conversion is better than the first and there should
be no ambiguity.

- Matthias Meixner

--
Matthias Meixner                   meixner@rbg.informatik.tu-darmstadt.de
Technische Universit   t Darmstadt
Rechnerbetriebsgruppe                          Telefon (+49) 6151 16 6670
Wilhelminenstra   e 7, D-64283 Darmstadt, Germany    Fax (+49) 6151 16 4701


---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 2000/04/08
Raw View

Matthias Meixner wrote:
>
> S
>       Z& -> const Z& (Qualification adjustment)
> const Z& -> const Z& (Identity)
>
> So the second conversion is better than the first and there should
> be no ambiguity.
>

The second is not better.   Qualification adjustment and Identity have the same rank
("Exact Match").

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: meixner@rbg.informatik.tu-darmstadt.de (Matthias Meixner)
Date: 2000/04/11
Raw View
Ron Natalie (ron@sensor.com) wrote:
:
:
: Matthias Meixner wrote:
: >
: > S
: >       Z& -> const Z& (Qualification adjustment)
: > const Z& -> const Z& (Identity)
: >
: > So the second conversion is better than the first and there should
: > be no ambiguity.
: >
:
: The second is not better.   Qualification adjustment and Identity have the same rank
: ("Exact Match").

13.3.3.2/3:
Standard conversion sequence S1 is a better conversion sequence S2 if
S1 is a proper subsequence of S2 (comparing the conversion sequences in
the canonical form defined by 13.3.3.1.1; the identity conversion sequence
is considered to be a subsequence of any non-identity conversion sequence)

const Z & -> const Z& (identity) is a subsequence of Z& -> const Z& and
thus it is a better conversion sequence.

- Matthias Meixner

--
Matthias Meixner                   meixner@rbg.informatik.tu-darmstadt.de
Technische Universit   t Darmstadt
Rechnerbetriebsgruppe                          Telefon (+49) 6151 16 6670
Wilhelminenstra   e 7, D-64283 Darmstadt, Germany    Fax (+49) 6151 16 4701


---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: meixner@rbg.informatik.tu-darmstadt.de (Matthias Meixner)
Date: 2000/04/06
Raw View
Hi,

once again I have a problem with user defined conversions and I am not
sure, whether the problem lies within the standard or if it simply is
a compiler bug.

Take the following short example:

-------------------------

struct Z { Z(){} };

struct A {
   Z x;

   operator Z *() { return &x; }
   operator const Z *() { return &x; }
   operator const Z *() const { return &x; }
};

struct B {
   Z x;

   operator Z &() { return x; }
   operator const Z &() { return x; }
   operator const Z &() const { return x; }
};

main()
{
   A a;
   const A ca;
   Z *a1=a;
   const Z *a2=a;
   const Z *a3=ca;

   B b;
   const B cb;
   Z &b1=b;
   const Z &b2=b;
   const Z &b3=cb;
}

-------------------------

Whereas everything works fine for A, for B gcc reports:

t5.cxx: In function `int main ()':
t5.cxx:31: conversion from `B' to `const Z &' is ambiguous
t5.cxx:15: candidates are: B::operator Z & ()
t5.cxx:16:                 B::operator const Z & ()
t5.cxx:17:                 B::operator const Z & () const
t5.cxx:31: conversion from `B' to `const Z' is ambiguous
t5.cxx:15: candidates are: B::operator Z & ()
t5.cxx:16:                 B::operator const Z & ()
t5.cxx:17:                 B::operator const Z & () const

Since A and B differ only in the return type, one being a pointer whereas
B returns a reference, I thought they should both work.

According to my understanding of 13.3.3 and 13.3.3.2 of the C++ standard
there should be no ambiguity. Or did I overlook something?

- Matthias Meixner

--
Matthias Meixner                   meixner@rbg.informatik.tu-darmstadt.de
Technische Universitdt Darmstadt
Rechnerbetriebsgruppe                          Telefon (+49) 6151 16 6670
Wilhelminenstra_e 7, D-64283 Darmstadt, Germany    Fax (+49) 6151 16 4701




---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]