Saturday, February 19, 2022

[SOLVED] Compiler Warn on enum

Issue

Is there a way to get the compiler to warn if an integer is outside the 'range' of an enum ? For example, something like this:

enum Brothers {Snee, Snoo, Snum};

int main(void) {

  enum Brothers k;
  k = Snee;
  k = 9; // compiler warning for an int outside [0,2] ?

}

Solution

Not for gcc as of version 10.2. There's a related open enhancement request from 2002 but developers haven't seemed interested in implementing it.

clang will warn about this if given the -Wassign-enum option: https://godbolt.org/z/zv8an5



Answered By - Nate Eldredge
Answer Checked By - Dawn Plyler (WPSolving Volunteer)