Namespaces
Variants

std:: underlying_type

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

underlying_type
(C++11)
(C++11)
(C++17)
Compile-time rational arithmetic
Compile-time integer sequences
Definiert im Header <type_traits>
template < class T >
struct underlying_type ;
(seit C++11)

Falls T ein vollständiger Aufzählungstyp (enum) ist, stellt es einen Member-Typedef type bereit, der den zugrundeliegenden Typ von T benennt.

Andernfalls ist das Verhalten undefiniert.

(bis C++20)

Andernfalls, wenn T kein Aufzählungstyp ist, gibt es kein Mitglied type . Andernfalls ( T ist ein unvollständiger Aufzählungstyp), ist das Programm fehlerhaft.

(seit C++20)

Wenn das Programm Spezialisierungen für std::underlying_type hinzufügt, ist das Verhalten undefiniert.

Inhaltsverzeichnis

Mitgliedertypen

Name Definition
type der zugrundeliegende Typ von T

Hilfstypen

template < class T >
using underlying_type_t = typename underlying_type < T > :: type ;
(seit C++14)

Hinweise

Jeder Aufzählungstyp hat einen zugrundeliegenden Typ , der sein kann

  1. Explizit angegeben (sowohl für scoped als auch unscoped Aufzählungen);
  2. Ausgelassen, in welchem Fall es int für scoped Aufzählungen oder ein implementierungsdefinierter integraler Typ, der alle Werte der Enum darstellen kann (für unscoped Aufzählungen), ist.

Beispiel

#include <iostream>
#include <type_traits>
enum e1 {};
enum class e2 {};
enum class e3 : unsigned {};
enum class e4 : int {};
int main()
{
    constexpr bool e1_t = std::is_same_v<std::underlying_type_t<e1>, int>;
    constexpr bool e2_t = std::is_same_v<std::underlying_type_t<e2>, int>;
    constexpr bool e3_t = std::is_same_v<std::underlying_type_t<e3>, int>;
    constexpr bool e4_t = std::is_same_v<std::underlying_type_t<e4>, int>;
    std::cout
        << "underlying type for 'e1' is " << (e1_t ? "int" : "non-int") << '\n'
        << "underlying type for 'e2' is " << (e2_t ? "int" : "non-int") << '\n'
        << "underlying type for 'e3' is " << (e3_t ? "int" : "non-int") << '\n'
        << "underlying type for 'e4' is " << (e4_t ? "int" : "non-int") << '\n';
}

Mögliche Ausgabe:

underlying type for 'e1' is non-int
underlying type for 'e2' is int
underlying type for 'e3' is non-int
underlying type for 'e4' is int

Fehlerberichte

Die folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.

DR Angewendet auf Verhalten wie veröffentlicht Korrektes Verhalten
LWG 2396 C++11 unvollständige Aufzählungstypen waren erlaubt vollständiger Aufzählungstyp erforderlich

Siehe auch

(C++11)
prüft, ob ein Typ ein Aufzählungstyp ist
(Klassentemplate)
prüft, ob ein Typ ein scoped enumeration type ist
(Klassentemplate)
konvertiert eine Aufzählung in ihren zugrundeliegenden Typ
(Funktionstemplate)