[Bug c++/14802] New: [3.3/3.4/3.5 Regression] Conditional expression with throw and void fails to compile

alexis at ortsa dot com gcc-bugzilla@gcc.gnu.org
Wed Mar 31 22:13:00 GMT 2004


This code does not compile with recent versions of g++-3.3:

int main() { 0 ? (void) 0: throw 0; }

It produces the following error:
error: void value not ignored as it ought to be

The standard, as included in the comments of gcc/gcc/cp/call.c states:
[expr.cond]
  If either the second or the third operand has type (possibly
  cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
  array-to-pointer (_conv.array_), and function-to-pointer
  (_conv.func_) standard conversions are performed on the second
  and third operands. 

  One of the following shall hold:
  --The second or the third operand (but not both) is a
    throw-expression (_except.throw_); the result is of the
    type of the other and is an rvalue.
  --Both the second and the third operands have type void; the
    result is of type void and is an rvalue.

As I understand it, both cases hold in this example, and so the result is of
type void and is an rvalue. The resulting value is not used, so I think that the
code is valid.

I believe that the behavior changed because of this patch:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/call.c.diff?cvsroot=gcc&r1=1.461&r2=1.462
that fixed bug 14083. The example compiles correctly on a version of g++-3.3.3
dating around February (I don't have the exact version, sorry), as it does with
g++-3.2.3.

The following patch should eliminate the compilation error without breaking the fix:
--- call.c.orig 2004-03-31 22:23:57.000000000 +0200
+++ call.c.new  2004-03-31 22:28:40.000000000 +0200
@@ -3182,14 +3182,16 @@
       if (TREE_CODE (arg2) == THROW_EXPR
          && TREE_CODE (arg3) != THROW_EXPR)
        {
-         arg3 = force_rvalue (arg3);
+         if (!VOID_TYPE_P (arg3_type))
+           arg3 = force_rvalue (arg3);
          arg3_type = TREE_TYPE (arg3);
          result_type = arg3_type;
        }
       else if (TREE_CODE (arg2) != THROW_EXPR
               && TREE_CODE (arg3) == THROW_EXPR)
        {
-         arg2 = force_rvalue (arg2);
+         if (!VOID_TYPE_P (arg2_type))
+           arg2 = force_rvalue (arg2);
          arg2_type = TREE_TYPE (arg2);
          result_type = arg2_type;
        }

It seems that force_rvalue returns an error when given an argument of type void.
I don't know if this behavior is correct, but it's easy to contourn in this case.



Sorry for my english, I hope everything is understandable.

-- 
           Summary: [3.3/3.4/3.5 Regression] Conditional expression with
                    throw and void fails to compile
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: alexis at ortsa dot com
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14802



More information about the Gcc-bugs mailing list