Topic: ambiguities with conversion operators (trying to do const-safe conversions)
Author: feb6399@osfmail.isc.rit.edu (Frank Barrus)
Date: 1997/02/14 Raw View
// Let's say you have a case like the following, with a struct
// that contains the internal data of a class, and is supposed
// to be useable any place you would normally have a reference
// to the class. (the struct is used so that static tables of
// these objects can be initialized without generating constructors
// for all the elements)
// Anyway, my question is--- if you can have two functions
// with the same parameters, overloaded only by whether they
// are 'const' functions or not (thus depending upon the
// 'const'ness of the object), how come you can't do that
// for conversion operators like the following:
class A;
struct _A
{
int x, y;
operator const A & () const
{ return *reinterpret_cast<const A*>(this); }
operator A & ()
{ return *reinterpret_cast<A*>(this); }
};
class A : _A
{
public:
A();
};
const _A xx = {1, 2};
void blah(const A &);
main()
{
blah(xx); // this is ambiguous (according to g++ 2.7.2)
// but shouldn't it obviously pick the const
// form of the conversion, since the object
// being used is const?
}
// Of course, the solution in this case seems to be to just remove the
// non-const version of the conversion, since I'm only using the
// struct for static initialization, but as a general rule, shouldn't
// conversion operators be allowed to exist in separate forms
// for both const and non-const objects, so that a const-safe
// conversion results?
// I realize the problem probably stems from the fact that overloading
// is done on functions with the same name, and technically the two
// operator conversions have different names, but they are really the
// same conversion, just with and without const for the return type.
// Is there perhaps a better way I should be doing this? How does
// one do const-safe conversion operators? (Note that using
// a constructor for 'A' would not be appropriate in this case,
// nor do I wish to explicitly cast every time I wish to convert)
--
Frank "Shaggy" Barrus: shaggy@csh.rit.edu; http://www.csh.rit.edu/~shaggy
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/02/16 Raw View
Frank Barrus writes:
> // Anyway, my question is--- if you can have two functions
> // with the same parameters, overloaded only by whether they
> // are 'const' functions or not (thus depending upon the
> // 'const'ness of the object), how come you can't do that
> // for conversion operators like the following:
You can.
> class A;
[snip]
> struct _A {
> operator const A & () const;
> operator A & ();
> };
[snip]
> const _A xx;
> void blah(const A &);
[snip]
> blah(xx); // this is ambiguous (according to g++ 2.7.2)
That's correct, g++ 2.7.2 is broken. The next major release of gcc
(2.8.0) should accept this.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]