Ada Programming/Attributes/'Enum Rep

Description
where  is an enumeration type and   is an instance of that type will return the underlying representation for that instance of the enumeration. The default representation of an enumeration is based on its position (starting at zero). However, Ada does provide language facilities for specifying the representation independently of the position. The Enum_Rep allows you to retrieve that representation. Generally in Ada, enumerations are their own type and the representation is not important. However, in the interests of cross language compatibility and for possible use in embedded programming the representation can be manipulated. While the core language allows you to change the representation, it doesn't provide a convenient attribute for retrieving it. This extended attribute addresses that need. Using it does require that you know the underlying type used to support the enumeration, since  returns that type. Typically, the standard type Integer is sufficient.

Note that this attribute is now standard in Ada 2022. In earlier Ada versions, it is available in GNAT as an implementation defined attribute, but the standard and portable way to get the internal representation was using an instantiation of Unchecked_Conversion.

Example
Enum_Type (Enum1, Enum2, Enum3); Enum_Val : Enum_Type := Enum1;

(Enum_Type'Enum_Rep(Enum_Val) = 0);

Wikibook

 * Ada Programming
 * Ada Programming/Attributes
 * Ada Programming/Attributes/'Enum_Val

Ada 2022 Overview

 * 7.13 Getting the representation of an enumeration value