LearnFrenzy
Sign in Take a free test

Abstract Classes and Interfaces · Predict the Output · Question 9

Which types satisfy a concept? Answer, explanation & discussion

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

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

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

template <typename T>
concept Sized = requires(const T& t) { { t.size() } -> std::convertible_to<std::size_t>; };

struct Box { int size() const { return 3; } };
struct Point { int x, y; };

int main() {
    std::cout << Sized<std::string> << Sized<std::vector<int>> << Sized<Box>
              << Sized<Point> << Sized<int> << '\n';
    return 0;
}
  • A 11100 Correct
  • B 11000
  • C 11110
  • D 00100
Correct Answer: Option A

Box has no base class but satisfies the concept because it has a size() whose result converts to size_t. Point and int have no size().

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