LearnFrenzy
Sign in Take a free test

Modern C++ Features (C++11 to C++23) · Modern C++ MCQ · Question 20

How std::any checks types Answer, explanation & discussion

  • MCQ
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 20 Back to Exercise

What happens with std::any a = 5; std::any_cast<double>(a);?

  • A It returns 5.0
  • B It throws std::bad_any_cast – the stored type is int, and any_cast requires the exact type Correct
  • C Compile error
  • D Undefined behaviour
Correct Answer: Option B

The pointer form any_cast<double>(&a) returns nullptr instead of throwing.

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