C++11—Specifying the Type of an enum’s Constants


The constants in an enum are represented as integers. By default, an unscoped enum’s underlying integral type depends on its constants’ values—the type is guaranteed to be large enough to store the constant values specified. By default, a scoped enum’s underlying integral type is int. C++11 allows you to specify an enum’s underlying integral type by following the enum’s type name with a colon (:) and the integral type. For example, we can specify that the constants in the enum class Status should have type unsigned int, as in

enum class Status : unsigned int { CONTINUE, WON, LOST };

Common ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.