Tool/software: TI C/C++ Compiler
I compiled the program below with the compiler option --enum_type=unpacked, --enum_type=packed and --enum_type=int.
But the size of enum_class with the compiler option --enum_type=packed is 2 bytes instead of 1 byte as expected.
In the same way, the size of enum_class_attribute_packed is always 2 bytes instead of 1 byte.
The size of enum_class_unsigned_char is always 1 byte as expected. But in this case I have a problem with the operator static_cast<enum>(int). See details here:
https://e2e.ti.com/support/tools/ccs/f/81/t/746141
Then how can I make to have an "enum class" with a size of 1 byte ?
#include <stdio.h>
#include <msp430.h>
/**
* hello.cpp
*/
enum class enum_class { red, green, blue };
enum class enum_class_attribute_packed { red, green, blue } __attribute__((__packed__));
enum class enum_class_unsigned_char: unsigned char { red, green, blue };
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
printf("sizeof(enum_class) = %d\r\n", sizeof(enum_class));
printf("sizeof(enum_class_attribute_packed) = %d\r\n", sizeof(enum_class_attribute_packed));
printf("sizeof(enum_class_unsigned_char) = %d\r\n", sizeof(enum_class_unsigned_char));
return 0;
}
/*
--enum_type=unpacked
Console:
sizeof(enum_class) = 2
sizeof(enum_class_attribute_packed) = 2
sizeof(enum_class_unsigned_char) = 1
*/
/*
--enum_type=packed
Console:
sizeof(enum_class) = 2
sizeof(enum_class_attribute_packed) = 2
sizeof(enum_class_unsigned_char) = 1
*/
/*
--enum_type=int
Console:
sizeof(enum_class) = 2
sizeof(enum_class_attribute_packed) = 2
sizeof(enum_class_unsigned_char) = 1
*/
/*
Compiler version: TI v18.1.4.LTS
Compiler flags:
-vmspx
--data_model=restricted
--use_hw_mpy=F5
--include_path="${CCS_BASE_ROOT}/msp430/include"
--include_path="${PROJECT_ROOT}"
--include_path="${CG_TOOL_ROOT}/include"
--advice:power="1,2,3,4,5.1,5.2,6,7,8,9,10,11,12,13,14,15"
--advice:hw_config=all
--define=__MSP430FR5994__
--define=_MPU_ENABLE
-g
--printf_support=minimal
--diag_warning=225
--diag_wrap=off
--display_error_number
--enum_type=unpacked
--silicon_errata=CPU21
--silicon_errata=CPU22
--silicon_errata=CPU40
*/
/*
Linker flags:
-m"${ProjName}.map"
--heap_size=300
--stack_size=160
--cinit_hold_wdt=on
-i"${CCS_BASE_ROOT}/msp430/include"
-i"${CCS_BASE_ROOT}/msp430/lib/5xx_6xx_FRxx"
-i"${CCS_BASE_ROOT}/msp430/lib/FR59xx"
-i"${CG_TOOL_ROOT}/lib"
-i"${CG_TOOL_ROOT}/include"
--priority
--reread_libs
--define=_MPU_ENABLE
--diag_wrap=off
--display_error_number
--warn_sections
--xml_link_info="${ProjName}_linkInfo.xml"
--use_hw_mpy=F5
--rom_model
*/