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

            Bug ID: 109532
           Summary: -fshort-enums does not pick smallest underlying type
                    for scoped enum
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: klaus.doldinger64 at googlemail dot com
  Target Milestone: ---

In the follwoing code, the best code with 8-bit underlying for the enum-type is
only optimized for unscoped enum.
The scoped enum always produces a 16-bit enum underlying type.

Compile with: -Os -mmcu=atmega328  -fshort-enums

(note: equivalent C code (unscoped enums) produces optimal code with
-fshort-enums) 

#include <avr/io.h>
#include <stdint.h>

volatile uint8_t o;

struct FSM {
//    enum class State : uint8_t {A, B, C, D};// <2> 8bit enum
//    enum State {A, B, C, D};                // <3> 8bit enum with
-fshort-enums
    enum class State {A, B, C, D};            // <1> 16bit enum with
-fshort-enums
    static void f() __attribute__((noinline)) {
        switch(mState) {
        case State::A:
            o = 10;
            break;
        case State::B:
            o = 11;
            break;
        case State::C:
            o = 12;
            break;
        case State::D:
            o = 13;
            break;
        }
    }
private:
    inline static State mState{State::A};
};

int main() {
    while(true) {
        FSM::f();
    }
}

Reply via email to