This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Allow cast from double to enumeration type in C++.
- From: gkeating at geoffk5 dot apple dot com (Geoffrey Keating)
- To: gcc-patches at gcc dot gnu dot org
- Date: Thu, 24 Mar 2005 15:29:05 -0800 (PST)
- Subject: Allow cast from double to enumeration type in C++.
static_cast permits you to perform the inverse of any standard
conversion sequence ([expr.static.cast] paragraph 6), and [conv.fpint]
paragraph 2 says that "An rvalue of an integer type or of an
enumeration type can be converted to an rvalue of a floating point
type"; therefore, static_cast permits you to cast a floating-point
type to an enumeration type.
(But you knew that already.)
Bootstrapped & tested on powerpc-darwin8.
--
- Geoffrey Keating <geoffk@apple.com>
===File ~/patches/gcc-4063366.patch=========================
Index: cp/ChangeLog
2005-03-24 Geoffrey Keating <geoffk@apple.com>
* typeck.c (build_static_cast_1): Allow scalar_cast between
any integral, floating, or enumeration type.
Index: testsuite/ChangeLog
2005-03-24 Geoffrey Keating <geoffk@apple.com>
* g++.dg/expr/cast3.C: New.
Index: cp/typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/typeck.c,v
retrieving revision 1.620
diff -u -p -u -p -r1.620 typeck.c
--- cp/typeck.c 22 Mar 2005 09:30:06 -0000 1.620
+++ cp/typeck.c 24 Mar 2005 23:25:38 -0000
@@ -4608,13 +4608,15 @@ build_static_cast_1 (tree type, tree exp
promotions, floating point promotion, integral conversions,
floating point conversions, floating-integral conversions,
pointer conversions, and pointer to member conversions. */
- if ((ARITHMETIC_TYPE_P (type) && ARITHMETIC_TYPE_P (intype))
- /* DR 128
-
- A value of integral _or enumeration_ type can be explicitly
- converted to an enumeration type. */
- || (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
- && INTEGRAL_OR_ENUMERATION_TYPE_P (intype)))
+ /* DR 128
+
+ A value of integral _or enumeration_ type can be explicitly
+ converted to an enumeration type. */
+ /* The effect of all that is that any conversion between any two
+ types which are integral, floating, or enumeration types can be
+ performed. */
+ if ((INTEGRAL_TYPE_P (type) || SCALAR_FLOAT_TYPE_P (type))
+ && (INTEGRAL_TYPE_P (intype) || SCALAR_FLOAT_TYPE_P (intype)))
{
expr = ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL);
Index: testsuite/g++.dg/expr/cast3.C
===================================================================
RCS file: testsuite/g++.dg/expr/cast3.C
diff -N testsuite/g++.dg/expr/cast3.C
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/g++.dg/expr/cast3.C 24 Mar 2005 23:26:04 -0000
@@ -0,0 +1,22 @@
+// { dg-do compile }
+
+enum MyState
+{
+ QUIT = 0,
+ START,
+ STOP,
+ PAUSE
+};
+
+double GetDouble()
+{
+ return 1.0;
+}
+
+int main()
+{
+ MyState the_state;
+
+ the_state = (MyState)GetDouble(); // { dg-bogus "invalid cast" }
+ return 0;
+}
============================================================