public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Dave Williss" <dwilliss@microimages.com>
To: <gcc-help@gcc.gnu.org>
Subject: Re: How do I avoid this warning?
Date: Fri, 19 Jul 2002 08:22:00 -0000	[thread overview]
Message-ID: <0b6301c22f38$1f5df300$2200000a@opus800> (raw)
In-Reply-To: <3D374C3C.57ACBD01@centropolisfx.com>


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

      reply	other threads:[~2002-07-19 15:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-07-18  8:01 Dave Williss
2002-07-18 16:14 ` Gokhan Kisacikoglu
2002-07-19  8:22   ` Dave Williss [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='0b6301c22f38$1f5df300$2200000a@opus800' \
    --to=dwilliss@microimages.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).