https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83929

            Bug ID: 83929
           Summary: implicit conversion of literal class type can not be
                    used as bit-field length
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: smw at gcc dot gnu.org
  Target Milestone: ---

[expr.const]/3 says "An integral constant expression is an expression of
integral or unscoped enumeration type, implicitly converted to a prvalue, where
the converted expression is a core constant expression. [ Note: Such
expressions
may be used as array bounds (8.3.4, 5.3.4), as bit-field lengths (9.6), as
enumerator initializers if the underlying type is not fixed (7.2), and as
alignments (7.6.2). — end note ]"

[expr.const]/6 says "If an expression of literal class type is used in a
context where an integral constant expression is required, then that expression
is contextually implicitly converted (Clause 4) to an integral or unscoped
enumeration type and the selected conversion function shall be constexpr."

That works on ICC, LLVM, MSVC, and other compilers, but....


    struct LiteralClassType
    {
      constexpr LiteralClassType(int i) : val(i) { }
      constexpr operator int() const { return val; }
    private:
      int val;
    };

    constexpr LiteralClassType field_width(3);

    struct StructWithBitfields
    {
        int f1:field_width;
    };

will give the following error under GCC (all versions tested).

14 : <source>:14:16: error: width of bit-field 'f1' has non-integral type
'const LiteralClassType'
         int f1:field_width;
                ^~~~~~~~~~~
Compiler returned: 1

Reply via email to