LearnFrenzy
Sign in Take a free test

Modern C++ Features (C++11 to C++23) · Find the Error · Question 5

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

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

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

main.cpp
#include <iostream>

consteval int kb(int n) { return n * 1024; }

int main() {
    int n;
    std::cin >> n;
    std::cout << kb(n) << '\n';
    return 0;
}
  • A kb must return long
  • B A consteval function must be evaluated at compile time, but n is read at run time; use constexpr instead (which may run at run time) Correct
  • C cin cannot read ints
  • D 1024 is too big
Correct Answer: Option B

kb(4) would be fine – its argument is a constant.

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