#include <iostream>
enum IsOptionAEnum
{
IsOptionA_YES,
IsOptionA_NO
};
enum IsOptionBEnum
{
IsOptionB_YES,
IsOptionB_NO
};
void TestFunc(IsOptionAEnum optionA, IsOptionBEnum optionB)
{
if (optionA == IsOptionA_YES || optionA == IsOptionB_YES) // typo
{
// ...
}
//if (optionA == IsOptionA_YES || optionB == IsOptionB_YES) // correct one
//{
//}
}
Question> optionA is of type IsOptionAEnum and doesn’t have the value of IsOptionB_YES. Why does the compiler of VS2010 not find this error?
If it is the case where compiler cannot find the error, is there a way that I can enforce this restriction so that compiler can find the error?
Whilst the standard doesn’t render this an error (enums are effectively syntax over integers), this is certainly something a compiler can detect. Clang, compiling with
-Wenum-compare, gives:It may be that Visual C++ doesn’t warn about this by default. Try setting the
/Wallflag on the compiler, which will enable all warnings. If it still doesn’t warn, you can file a request with the VC compiler team.Edit: As other answers and comments have mentioned, if you have a VC11, you can use Strongly typed enums.