Question 9
Back to Exercise
What is the output of the following C++ program on a 64-bit system?
#include <iostream>
#include <type_traits>
enum class Level : unsigned char { low = 1, mid = 5, high = 10 };
enum class Http { ok = 200, not_found = 404 };
int main() {
auto l = Level::mid;
std::cout << static_cast<int>(l) << ' ' << sizeof(Level) << ' ' << sizeof(Http) << ' '
<< static_cast<int>(Http::not_found) << ' ' << std::to_underlying(Level::high) + 0 << ' '
<< (Level::low < Level::high) << '\n';
return 0;
}
- A 5 1 4 404 10 1 Correct
- B 5 4 4 404 10 1
- C 1 1 4 404 10 1
- D 5 1 4 404 10 0
Correct Answer: Option A
The underlying type sets the size (unsigned char → 1 byte; the default is int). Enumerators of the same enum class can be compared; std::to_underlying (C++23) converts to the underlying type.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!