LearnFrenzy
Sign in Take a free test

Modern C++ Features (C++11 to C++23) · Predict the Output · Question 12

Defaulted comparisons and std::tie Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 12 Back to Exercise

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

struct Release {
    int major, minor, patch;
    std::string tag;
    auto operator<=>(const Release&) const = default;
    bool operator==(const Release&) const = default;
};

int main() {
    std::vector<Release> r{{1, 4, 0, "b"}, {1, 10, 2, "a"}, {1, 4, 0, "a"}, {0, 9, 9, "z"}};
    std::sort(r.begin(), r.end());
    for (const auto& x : r) std::cout << x.major << '.' << x.minor << '.' << x.patch << x.tag << ' ';
    std::cout << (r[1] == Release{1, 4, 0, "b"}) << '\n';
    return 0;
}
  • A 0.9.9z 1.4.0a 1.4.0b 1.10.2a 0 Correct
  • B 0.9.9z 1.10.2a 1.4.0a 1.4.0b 0
  • C 0.9.9z 1.4.0b 1.4.0a 1.10.2a 1
  • D 1.10.2a 1.4.0a 1.4.0b 0.9.9z 0
Correct Answer: Option A

Defaulted <=> compares members in order, numerically (so 10 > 4, unlike string comparison), then the tag. r[1] is 1.4.0a, not 1.4.0b.

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