Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) enum

 

enum is a keyword to define an enumeration. Use enumerations to represent sets of named constants [7].

 

Define operations on enumerations for safe and simple use [9]. Avoid enumerations at file scope in header files [5]. Use a consistent method to identify immutable values such as enum values [6].

 

In C++11, prefer class enums over plain enums to minimize surprise [8].

 

 

 

 

 

C++98C++11 Example: with and without enum

 

Below is an example of a code that did not use enum.

 

//Without using enum, not preferred
void sayHello(const int& sex)
{
  switch(sex)
  {
    case 0: std::cout << "(male voice) Hello!" << std::endl; break;
    case 1: std::cout << "(female voice) Hello!" << std::endl; break;
    case 2: std::cout << "(hermaphrodite voice) Hello!" << std::endl; break;
  }
}

 

How can one expect to memorize all these values for sexes? enum relieves things:

 

enum Sex { male, female, hermaphrodite };

void sayHello(const Sex& sex)
{
  switch(sex)
  {
    case male: std::cout << "(male voice) Hello!" << std::endl; break;
    case female: std::cout << "(female voice) Hello!" << std::endl; break;
    case hermaphrodite: std::cout << "(hermaphrodite voice) Hello!" << std::endl; break;
  }
}

 

Of course, the example without enum can be converted to the example below, using global constants. Prefer not to use globals [1-4].

 

//Without using enum, not preferred
const int male = 0; //Not preferred
const int female = 1; //Not preferred
const int hermaphrodite = 2; //Not preferred

void sayHello(const int& sex)
{
  switch(sex)
  {
    case male: std::cout << "(male voice) Hello!" << std::endl; break;
    case female: std::cout << "(female voice) Hello!" << std::endl; break;
    case hermaphrodite: std::cout << "(hermaphrodite voice) Hello!" << std::endl; break;
  }
}

 

 

 

 

 

C++98C++11 Example how to define an enum and overloading operator<< for it

 

#include <iostream>

enum Sign { minus = -1, none = 0, plus = 1};

std::ostream& operator<<(std::ostream& os, const Sign& s)
{
  os << (s == minus ? "-" : (s == none ? " " : "+") );
  return os;
}

int main()
{
  const Sign a = plus;
  const Sign b = none;
  const Sign c = minus;
  std::cout
    << "Plus : " << a
    << "\nNo sign: " << b
    << "\nMinus : " << c
    << std::endl;
}

 

 

 

 

 

Advice

 

 

 

 

 

 

References

 

  1. Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. 2005. ISBN: 0-32-111358-6. Chapter 10: 'Minimize global and shared data'
  2. Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Chapter 1.8.2.a: 'Don't use global data (use members)'
  3. Jarrod Hollingworth, Bob Swart, Mark Cashman, Paul Gustavson. Sams C++ Builder 6 Developer's Guide. 2002. ISBN:0-672-32480-6. Chapter 3: 'Avoid using global variables'
  4. Scott Meyers. Effective C++ (3rd edition). ISBN: 0-321-33487-6. Item 2: Prefer consts, enums and inlines to #defines
  5. John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Chapter 2.3.3: 'Avoid enumerations, typedefs and constants at file scope in .h files'
  6. John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Chapter 2.7: 'Use a consistent method (such as all uppercase with underscores) to identify immutable values such as enumerators, const data and preprocessor constants'
  7. Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 8.5. Advice. page 224: '[5] Use enumerations to represent sets of named constants'
  8. Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 8.5. Advice. page 224: '[6] Prefer class enums over "plain" enums to minimize surprises'
  9. Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 8.5. Advice. page 224: '[7] Define operations on enumerations for safe and simple use'
  10. Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 8.5. Advice. page 224: '[6] Prefer class enums over "plain" enums to minimize surprises'
  11. Scott Meyers. C++ And Beyond 2012 session: 'Initial thoughts on Effective C++11'. 2012. ' Prefer enum classes to enums'

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict