This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: How do I avoid this warning?


----- Original Message -----
From: "Gokhan Kisacikoglu" <kisa@centropolisfx.com>
To: "Dave Williss" <dwilliss@microimages.com>
Cc: <gcc-help@gcc.gnu.org>
Sent: Thursday, July 18, 2002 6:16 PM
Subject: Re: How do I avoid this warning?


> Dave Williss wrote:
> >
> > I get a lot of warnings of the type:
> >
> >     warning: choosing `FOO::operator BAR*()` over `FOO::operator const
> > BAR*()
> >         for conversion from FRED to const BAR*
> >         because conversion sequence for the argument is better
> >
>
> Who is FRED? I guess you meant FOO! ;)

Actually, I ran out of TLAs for example class names.  FRED is really derived
from BAR.

>
> * Anyway, I guess the following would work better;
>
> FOO::operator BAR*() // non-const cases
> FOO::operator const BAR*() const // for const cases
>
> This will always use BAR * if FOO is not const, and const BAR * whenever
> FOO is const. This is at least more consistent...
>

This is what it's doing already and what's causing the warning...
Perhaps I simplified my example too much.
More details:

    template <size_t _T> class BAR {
        public:
            // constructors and other stuff omitted from email to save space

            operator const char* () const { return m_str; }
            operator char* () { return m_str; }
            // A bunch more methods and assignment operators follow...
        private:
            char m_str[_T];
        };

In this case, both methods return m_str.  The fact that there was a non-cost
version to begin with was a mistake, but the class is exported from a shared
library, and there is code depending on the non-const method still existing.
We still have some code outside the shared lib that requires the non-const
version to compile, but that code is really in error too.
We decided to just #ifdef the non-const version so that it's only defined
when
the shared library is being built.  That will force other code to take the
const
version and will probibably expose some evil code that also needed to be
const.

> * If you need to do explicitely convert to const BAR * from a non-const
> FOO, then maybe you should have an explicit method for that;
>
> const BAR * FOO::asBAR() const
>
> FOO *f = new FOO;
> const BAR *constb = f->asBar();
> BAR *b = f; // guaranteed to return BAR * in this context
>
> BTW, if your destructors are properly defined as virtual, then deleting
> a BAR * should never be a problem...
>

Actually, since BAR* was really just char* renamed to make the
example more generic, it doesn't have a virtual destructor.  In my
original example, I supposed that the non-const version might return

        strdup(m_bar)

instead, which would have to be freed with free, not delete.  But
this was just a hypothetical example where you would want the
warning to be an error.

> HTH,

What does HTH mean?
> Gokhan


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]