public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* How do I avoid this warning?
@ 2002-07-18  8:01 Dave Williss
  2002-07-18 16:14 ` Gokhan Kisacikoglu
  0 siblings, 1 reply; 3+ messages in thread
From: Dave Williss @ 2002-07-18  8:01 UTC (permalink / raw)
  To: gcc-help

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

How do I code so that the conversion won't be better?

I was going to ask if there was an option to disable this warning, but I can
see a case where this really would be incorrect behavior.  If the const
operator returns a pointer to internal data directly and the non-const has
to make a copy that the caller is expected to free, calling the non-const
method when the const was intended would give unexpected results (and leak
memory). This is just a hypothetical situation, but supports the need for
the warning.

So how do I force it to behave and call the correct method?

 -- Dave Williss
------
Meddle not in the affairs of dragons,
   for you are crunchy and taste good with catsup


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How do I avoid this warning?
  2002-07-18  8:01 How do I avoid this warning? Dave Williss
@ 2002-07-18 16:14 ` Gokhan Kisacikoglu
  2002-07-19  8:22   ` Dave Williss
  0 siblings, 1 reply; 3+ messages in thread
From: Gokhan Kisacikoglu @ 2002-07-18 16:14 UTC (permalink / raw)
  To: Dave Williss; +Cc: gcc-help

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! ;)

* 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...

* 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...

HTH,
Gokhan

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How do I avoid this warning?
  2002-07-18 16:14 ` Gokhan Kisacikoglu
@ 2002-07-19  8:22   ` Dave Williss
  0 siblings, 0 replies; 3+ messages in thread
From: Dave Williss @ 2002-07-19  8:22 UTC (permalink / raw)
  To: gcc-help


----- 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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-07-19 15:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-18  8:01 How do I avoid this warning? Dave Williss
2002-07-18 16:14 ` Gokhan Kisacikoglu
2002-07-19  8:22   ` Dave Williss

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).