Topic: Overload resolution question


Author: Bill Thompson <bill.t@ibm.net>
Date: 1999/09/08
Raw View
wmm@fastdial.net wrote:
>
> In article <7qmj41$7sc@news.dns.microsoft.com>,
>   "Michael" <mvl@rocketmail.com> wrote:
> > Suppose we have
> > class A
> > {
> >   operator char *() {}
> >   operator const char *() const {}
> > };
> > int main()
> > {
> >   A a;
> >   const char * s = a;
> > }
> >
> > Which of the 2 operators is called according to Standard?
> > Thanks in advance
>
> This calls A::operator char*().  According to 13.3.3p1, the  target
> type of a conversion function is considered last in determining the
> better of two functions, after all the other criteria (argument
> differences, template vs. non-template, and degree of specialization
> between template functions) have failed to distinguish them.  Since
> the non-const "a" is a better match for the implicit "this" parameter
> of the non-const conversion function than for the const conversion
> function, the difference in return types is not considered.
> --

I'm trying to muttle thru this issue myself, so my question would
be concerning the name look up portion of the standard 3.4.3.1
where it speaks of the _entire postfix expression_ of _conversion
type id_ ; does this _not_ include the _const_  in _const char*_
as far as name look up goes ?



--

       Regards,
  Bill T.


=================================================
If it was working before, and is not working now;
What did you change ?
=================================================
---
[ 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: wmm@fastdial.net
Date: 1999/09/08
Raw View
In article <37D5A78C.3F7BD38C@ibm.net>,
  Bill Thompson <bill.t@ibm.net> wrote:
> wmm@fastdial.net wrote:
> >
> > In article <7qmj41$7sc@news.dns.microsoft.com>,
> >   "Michael" <mvl@rocketmail.com> wrote:
> > > Suppose we have
> > > class A
> > > {
> > >   operator char *() {}
> > >   operator const char *() const {}
> > > };
> > > int main()
> > > {
> > >   A a;
> > >   const char * s = a;
> > > }
> > >
> > > Which of the 2 operators is called according to Standard?
> > > Thanks in advance
> >
> > This calls A::operator char*().  According to 13.3.3p1, the  target
> > type of a conversion function is considered last in determining the
> > better of two functions, after all the other criteria (argument
> > differences, template vs. non-template, and degree of specialization
> > between template functions) have failed to distinguish them.  Since
> > the non-const "a" is a better match for the implicit "this" parameter
> > of the non-const conversion function than for the const conversion
> > function, the difference in return types is not considered.
> > --
>
> I'm trying to muttle thru this issue myself, so my question would
> be concerning the name look up portion of the standard 3.4.3.1
> where it speaks of the _entire postfix expression_ of _conversion
> type id_ ; does this _not_ include the _const_  in _const char*_
> as far as name look up goes ?

There's no name lookup here.  The section you mentioned only comes
into play if the operator is invoked explicitly, e.g., if he had
written "a.operator const char*()".  In that case the "const" is part
of the "name" that is looked up.

In the case of an implicit conversion, though, the candidate
functions are found not by name lookup but by the process described
in 13.3.1.5, which basically gives all the conversion functions in
the class whose return type can be converted to the target type via
a standard conversion sequence.  In this example, both conversion
functions are candidate functions since both types can be converted
to the target type, "const char*".

--
William M. Miller, wmm@fastdial.net
Software Emancipation Technology (www.setech.com)


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.


[ 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: Bill Thompson <bill.t@ibm.net>
Date: 1999/09/08
Raw View


> In the case of an implicit conversion, though, the candidate
> functions are found not by name lookup but by the process described
> in 13.3.1.5, which basically gives all the conversion functions in
> the class whose return type can be converted to the target type via
> a standard conversion sequence.  In this example, both conversion
> functions are candidate functions since both types can be converted
> to the target type, "const char*".
>

Thanks, I am becomming convinced, I have a compiler bug.
This certainally doesn't led itself to the intuitive
element :).

--

       Regards,
  Bill T.


=================================================
If it was working before, and is not working now;
What did you change ?
=================================================



[ 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: "Michael" <mvl@rocketmail.com>
Date: 1999/09/03
Raw View
Suppose we have
class A
{
  operator char *() {}
  operator const char *() const {}
};
int main()
{
  A a;
  const char * s = a;
}

Which of the 2 operators is called according to Standard?
Thanks in advance
---
[ 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: wmm@fastdial.net
Date: 1999/09/03
Raw View
In article <7qmj41$7sc@news.dns.microsoft.com>,
  "Michael" <mvl@rocketmail.com> wrote:
> Suppose we have
> class A
> {
>   operator char *() {}
>   operator const char *() const {}
> };
> int main()
> {
>   A a;
>   const char * s = a;
> }
>
> Which of the 2 operators is called according to Standard?
> Thanks in advance

This calls A::operator char*().  According to 13.3.3p1, the  target
type of a conversion function is considered last in determining the
better of two functions, after all the other criteria (argument
differences, template vs. non-template, and degree of specialization
between template functions) have failed to distinguish them.  Since
the non-const "a" is a better match for the implicit "this" parameter
of the non-const conversion function than for the const conversion
function, the difference in return types is not considered.
--
William M. Miller, wmm@fastdial.net
Software Emancipation Technology (www.setech.com)


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
---
[ 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: whbloodworth@usa.net (William H. Bloodworth)
Date: 1999/09/03
Raw View
On 03 Sep 99 05:38:12 GMT, "Michael" <mvl@rocketmail.com> wrote:

>Suppose we have
>class A
>{
>  operator char *() {}
>  operator const char *() const {}
>};
>int main()
>{
>  A a;
>  const char * s = a;
>}
>
>Which of the 2 operators is called according to Standard?
>Thanks in advance

In your example...neither.  Both are private and "const char * s = a" would
not call either of the two operators.  Run this example:

#include <iostream>
#include <cstdlib>

class A
{
public:
   operator char *() { return "char *"; }
   operator const char *() const { return "const char *"; }
};

void Foo (const char * p) { std::cout << p << std::endl; }

int main (void)
{
   A a;
   Foo(a);
   return EXIT_SUCCESS;
}
---
[ 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: Stefan Schwarzer <sts@boa.ica1.uni-stuttgart.de>
Date: 1999/09/03
Raw View
Michael <mvl@rocketmail.com> wrote:
> Suppose we have
> class A
> {
>   operator char *() {}
>   operator const char *() const {}
> };
> int main()
> {
>   A a;
>   const char * s = a;
> }

> Which of the 2 operators is called according to Standard?
> Thanks in advance

In your example, both are private and thus none of them can be called ;)
If they were public then the non-const version is selected
because
 (i)  both functions are candidates
 (ii) operator char *()  does not require cv conversion
   on the implicit object argument

Return types are not considered during overload resolution step.
In your example above they only determine whether the function/operator
can become a candidate function for the overload resolution step.

--
Stefan Schwarzer                office:  +49-(0)711-685-7606   fax: x-3658
Uni Stuttgart, ICA 1            e-mail:          sts@ica1.uni-stuttgart.de
Pfaffenwaldring 27              pgp public key available on request/finger
70569 Stuttgart, Germany             http://www.ica1.uni-stuttgart.de/~sts
---
[ 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: jsa@edg.com (J. Stephen Adamczyk)
Date: 1999/09/03
Raw View
In article <7qmj41$7sc@news.dns.microsoft.com> "Michael" <mvl@rocketmail.com> writes:
>Suppose we have
>class A
>{
>  operator char *() {}
>  operator const char *() const {}
>};
>int main()
>{
>  A a;
>  const char * s = a;
>}
>
>Which of the 2 operators is called according to Standard?

Well, according the the standard, you get an error because the
conversion functions are inaccessible :-)

But you probably wanted to know which one would be called if
they were accessible.  The answer is "operator char *".
Overload resolution works first with the arguments of the
conversion functions, and there the non-const conversion
function looks better on the "this" parameter than does the
const conversion function.  There is another phase that
compares the standard conversion that is required after
the conversion function, but you only get to that phase
if the conversion functions were exactly as good as one
another on the argument match-up, and in this case they
aren't.

Steve Adamczyk



[ 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: "Michael" <mvl@rocketmail.com>
Date: 1999/09/03
Raw View
Of course I forgot to insert public. Thanks for all constructive responses.




[ 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: mlg@scr.siemens.com (Michael Greenberg)
Date: 1996/05/02
Raw View
Given

class foo;
void f(foo*) ;
void f(const foo*) ;
void xyz() { f(0); }

Which 'f' is supposed to be called from within 'xyz'?
--
Michael Greenberg                      email: mgreenberg@scr.siemens.com
Siemens Corporate Research             phone: 609-734-3347
755 College Road East                  fax: 609-734-6565
Princeton, NJ 08540


[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Rich Paul <linguist@cyberspy.com>
Date: 1996/05/06
Raw View
Michael Greenberg wrote:
>
> Given
>
> class foo;
> void f(foo*) ;
> void f(const foo*) ;
> void xyz() { f(0); }
>
> Which 'f' is supposed to be called from within 'xyz'?

I'd have to say d) none of the above.

Looks pretty ambiguous to me ...


> [ 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         ]
> [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c+
+/faq.html    ]
> [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c+
+/policy.html ]
> [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]

--
#include <legalbs/standarddisclaimer>
Rich Paul                |  If you like what I say,
tell my
C++, OOD, OOA, OOP,      |  employer, but if you
don't,
OOPs, I forgot one ...   |  don't blame them.  ;->