LearnFrenzy
Sign in Take a free test

Functions and Default Arguments · Find the Error · Question 8

Error: consteval function called with a run-time value Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links