diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index f3dc80c40cfad7dbc5a037e28d70e0c8c1bc9721..3b719326d76c2fe74111b46354d37af95098919d 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -8405,6 +8405,13 @@ build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p, return expr; if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) expr = TREE_OPERAND (expr, 0); + /* [expr.static.cast]: "If the value is not a bit-field, the result + refers to the object or the specified base class subobject thereof; + otherwise, the lvalue-to-rvalue conversion is applied to the + bit-field and the resulting prvalue is used as the operand of the + static_cast." There are no prvalue bit-fields; the l-to-r conversion + will give us an object of the underlying type of the bit-field. */ + expr = decay_conversion (expr, complain); return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain); } diff --git a/gcc/testsuite/g++.dg/cpp0x/scoped_enum12.C b/gcc/testsuite/g++.dg/cpp0x/scoped_enum12.C new file mode 100644 index 0000000000000000000000000000000000000000..1d10431e6dc4e525f0af6852b216f7d2fb183b32 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/scoped_enum12.C @@ -0,0 +1,8 @@ +// PR c++/111895 +// { dg-do compile { target c++11 } } + +enum class o_field : unsigned char { no, yes, different_from_s }; +struct fields { + o_field o : 2; +}; +bool func(fields f) { return static_cast<bool>(f.o); }