LearnFrenzy
Sign in Take a free test

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

Error: wrong number of names in a structured binding Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

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

main.cpp
#include <iostream>
#include <tuple>

std::tuple<int, int, int> rgb() { return {255, 128, 0}; }

int main() {
    auto [r, g] = rgb();
    std::cout << r << ' ' << g << '\n';
    return 0;
}
  • A tuple needs <utility>
  • B The tuple has 3 elements, so the structured binding must declare exactly 3 names: auto [r, g, b] = rgb(); Correct
  • C r and g must be int
  • D rgb must return a struct
Correct Answer: Option B

There is no way to skip elements (use [[maybe_unused]] or std::get for one element).

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