public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: John Love-Jensen <eljay@adobe.com>
To: Shriramana Sharma <samjnaa@gmail.com>, MSX to GCC <gcc-help@gcc.gnu.org>
Subject: Re: cast to an enum does not throw errors even for invalid values
Date: Tue, 22 May 2007 17:09:00 -0000	[thread overview]
Message-ID: <C2785377.220E0%eljay@adobe.com> (raw)
In-Reply-To: <464B4F64.70603@gmail.com>

Hi Shriramana,

> If an error is not called, I feel it defeats the very meaning of
> casting, to convert an object of one type to an object of another type.
> When there is no equivalent object of the target type, how can the
> casting happen?

It appears you have misunderstood what a cast operation means in this
situation.  Both the C-style cast, and the static_cast.

It means that you, the programmer, have vouched that the value being cast is
appropriate for what is being cast to.

That, obviously, is NOT what you desire.  You want some insurance that the
right hand value is checked and verified to be cast to the left hand BODY
value.

You should write and use this routine:

BODY ConvertToBODY(int i)
{
  BODY result = SUN;

  switch(i)
  {
    case SUN: result = SUN; break;
    case MOON: result = MOON; break;
    case STAR: result = STAR; break;
    default: throw "out of BODY range error";
  }

  return result;
}

It is, perhaps, unfortunate that the compiler does not synthesize this for
you.  But that's just not C++.

HTH,
--Eljay

  reply	other threads:[~2007-05-22 13:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-05-22 13:00 Shriramana Sharma
2007-05-22 17:09 ` John Love-Jensen [this message]
2007-05-28 14:17   ` Shriramana Sharma
2007-05-28 17:33     ` Sergei Organov
2007-05-29  0:23     ` John Love-Jensen
2007-05-29 11:05       ` Gabriel Dos Reis
2007-05-25 11:26 Shriramana Sharma
2007-05-25 14:38 ` Ian Lance Taylor
2007-05-25 16:54 ` Young, Michael

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=C2785377.220E0%eljay@adobe.com \
    --to=eljay@adobe.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=samjnaa@gmail.com \
    /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).