Question 8
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
#include <iostream>
consteval int square(int x) { return x * x; }
int main() {
int n;
std::cin >> n;
std::cout << square(n) << '\n';
return 0;
}
- A square must be recursive
- B A consteval function must be evaluated at compile time, but n is only known at run time; use constexpr instead Correct
- C cin cannot read int
- D consteval functions cannot return int
Correct Answer: Option B
constexpr would allow both: compile-time evaluation for constants and normal run-time calls for n.
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!