- Condition MUST evaluate to an integral type (
int,char,short,long, orenum/enum class). casevalues must be compile-time constant integral expressions.- Duplicate
casevalues are disallowed. default:handles all unmatched values.
main.cpp
#include <iostream>
enum class HttpStatus {
OK = 200,
NotFound = 404,
InternalError = 500
};
void handleResponse(HttpStatus status) {
switch (status) {
case HttpStatus::OK:
std::cout << "Request successful [200 OK]
";
break;
case HttpStatus::NotFound:
std::cout << "Resource not located [404 Not Found]
";
break;
case HttpStatus::InternalError:
std::cout << "Server crashed [500 Internal Error]
";
break;
default:
std::cout << "Unknown HTTP status
";
break;
}
}
int main() {
handleResponse(HttpStatus::OK);
handleResponse(HttpStatus::NotFound);
return 0;
}