Topic: static_cast between enumerations legal?


Author: juergen@monocerus.demon.co.uk (Juergen Heinzl)
Date: 1999/10/31
Raw View
In article <HG_R3.185$hp5.10973@dfiatx1-snr1.gtei.net>, Ron Burk wrote:
>Is the following code legal?
>
>  -------------------------------------
>  enum Type1 {value1, value2};
>  enum Type2 {value3, value4};
>
>
>  void foo(void)
>      {
>      Type2  a;
>
>      a =3D static_cast<Type2>(value1);
>      }
>  ---------------------------------------------
>
>Microsoft Visual C++ v6.0 and Borland C++ v5.4 both
>accept it without complaint. However, both object (VC++
>treats as error, Borland C++ only a warning) to
>a declaration like:
>
>    Type2 t(value1);
>
>This being relevant because of the wording of 5.2.9.2.
>
>The version of g++ I have refuses to compile the static_cast.
>Weirdly, however, it lets you flat-out assign
>between two different enumeration types and only
>gives a warning!

Not the latest version, but as both have the same range this --
a =3D Type2(value1);
-- is fine; at least if my interpretation of the docs is correct,
that is.
[...]

Cheers,
Juergen

--=20
\ Real name     : J=FCrgen Heinzl                 \       no flames      =
/
 \ EMail Private : juergen@monocerus.demon.co.uk \ send money instead /
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Gene Bushuyev" <gbush@my-deja.com>
Date: 1999/10/31
Raw View
Ron Burk <ronburk@gte.net> wrote in message
news:HG_R3.185$hp5.10973@dfiatx1-snr1.gtei.net...
> Is the following code legal?
>
>   -------------------------------------
>   enum Type1 {value1, value2};
>   enum Type2 {value3, value4};
>
>
>   void foo(void)
>       {
>       Type2  a;
>
>       a = static_cast<Type2>(value1);
>       }
>   ---------------------------------------------

The code is perfectly legal. In 7.2p1 section of the standard it is said
that "the identifiers in an enumerator-list are declared as constants" (of
the underlying type). Next, go to the section 7.2p9 and you find out that
explicit conversion from arithmetic or enumeration types are permitted. It
means that Type2 a(value1) is perfectly legal.
Section 5.2.9p2 provides the requirements for static_cast to be well formed.
Since the above mentioned declaration is well formed, the
static_cast<Type2>(value1) is also well formed.

Gene Bushuyev
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: jsa@edg.com (J. Stephen Adamczyk)
Date: 1999/10/31
Raw View
In article <HG_R3.185$hp5.10973@dfiatx1-snr1.gtei.net> "Ron Burk" <ronburk@gte.net> writes:
>Is the following code legal?
>
>  -------------------------------------
>  enum Type1 {value1, value2};
>  enum Type2 {value3, value4};
>
>
>  void foo(void)
>      {
>      Type2  a;
>
>      a = static_cast<Type2>(value1);
>      }
>  ---------------------------------------------
>
>My intepretation of the standard is that it is not legal to
>assign between two different enumeration types, and it
>is also not legal to static_cast<> from one enumeration
>type to another.

This was discussed at the J16/WG21 meeting last week. The
consensus is that static_cast between enumerations was
intended to be allowed, and words to make it so have been
proposed.  (This isn't an official interpretation yet, but
there didn't seem to be any controversy.)

Steve Adamczyk
Edison Design Group
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: juergen@monocerus.demon.co.uk (Juergen Heinzl)
Date: 1999/10/31
Raw View
In article <092184654201ea9CPIMSSMTPE01@msn.com>, Gene Bushuyev wrote:
>Ron Burk <ronburk@gte.net> wrote in message
>news:HG_R3.185$hp5.10973@dfiatx1-snr1.gtei.net...
>> Is the following code legal?
>>
>>   -------------------------------------
>>   enum Type1 {value1, value2};
>>   enum Type2 {value3, value4};
>>
>>
>>   void foo(void)
>>       {
>>       Type2  a;
>>
>>       a =3D static_cast<Type2>(value1);
>>       }
>>   ---------------------------------------------
>
>The code is perfectly legal. In 7.2p1 section of the standard it is said
>that "the identifiers in an enumerator-list are declared as constants" (=
of
>the underlying type). Next, go to the section 7.2p9 and you find out tha=
t
>explicit conversion from arithmetic or enumeration types are permitted. =
It
>means that Type2 a(value1) is perfectly legal.

Now "from arithmetic or enumeration types" is not the same as "to", so is
there this legal "now", aside from the easy way out I took.

>Section 5.2.9p2 provides the requirements for static_cast to be well for=
med.
>Since the above mentioned declaration is well formed, the
>static_cast<Type2>(value1) is also well formed.

Or does this imply "to" too and I am just nit picking ? Yes, I know given
the current available compilers this is a bit academic but curious even s=
o.

Thanks,
Juergen

--=20
\ Real name     : J=FCrgen Heinzl                 \       no flames      =
/
 \ EMail Private : juergen@monocerus.demon.co.uk \ send money instead /
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/10/31
Raw View
Gene Bushuyev wrote:
....
> The code is perfectly legal. In 7.2p1 section of the standard it is said
> that "the identifiers in an enumerator-list are declared as constants" (of
> the underlying type). ...

No - see 7.2p4:
"Following the closing brace of an _enum-specifier_, each enumerator has
the type of its enumeration. Prior to the closing brace, the type of
each enumerator is the type of its initializing value. If an initializer
is specified for an enumerator, the initializing value has the same type
as the expression. If no initializer is specified for the first
enumerator, the type is an unspecifiec integral type. Otherwise, the
type is the same as the type of the initializing value of the preceding
enumerator unless the incremented value is not representable in that
type, in which case the type is an unspecified integral type sufficient
to contain the incremented value."

Most of that complication applies only inside the enumeration
declaration; outside the declaration, the rule is simply that each
enumerator has the type of its enumeration - not of the underlying
integer type. You're remembering the way they work in C. It was changed
in C++ in order to make function overloads and template type deduction
work as they should.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Ron Burk" <ronburk@gte.net>
Date: 1999/10/30
Raw View
Is the following code legal?

  -------------------------------------
  enum Type1 {value1, value2};
  enum Type2 {value3, value4};


  void foo(void)
      {
      Type2  a;

      a = static_cast<Type2>(value1);
      }
  ---------------------------------------------

Microsoft Visual C++ v6.0 and Borland C++ v5.4 both
accept it without complaint. However, both object (VC++
treats as error, Borland C++ only a warning) to
a declaration like:

    Type2 t(value1);

This being relevant because of the wording of 5.2.9.2.

The version of g++ I have refuses to compile the static_cast.
Weirdly, however, it lets you flat-out assign
between two different enumeration types and only
gives a warning!

My intepretation of the standard is that it is not legal to
assign between two different enumeration types, and it
is also not legal to static_cast<> from one enumeration
type to another. IOW, I think every compiler I use is
wrong on this issue in one way or another (they sure
can't all be right :-). Have I missed something?

 Ron Burk
 Windows Developer's Journal, www.wdj.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]