Modern C++ Features (C++17/20) Flashcards
7 cards from real CPP practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Modern C++ Features (C++17/20) flashcards as text
What does the C++20 three-way comparison operator `` (spaceship operator) return when comparing two ints?
Answer: std::strong_ordering
Comparing ints with produces std::strong_ordering because integers have a total order with no equivalent-but-unequal values.
Which C++20 feature allows `Point p = {.x = 1, .y = 2};` initialization syntax?
Answer: Designated initializers
C++20 designated initializers let you name aggregate members by their field names in initialization, matching C99's syntax.
How does `consteval` differ from `constexpr` in C++20?
Answer: consteval requires evaluation at compile time; constexpr permits runtime evaluation
A consteval function (immediate function) must be called in a constant expression context; it cannot be invoked at runtime unlike constexpr.
What does `std::format("{:>10}", "hi")` produce in C++20?
Answer: " hi" (right-aligned in a field of width 10)
> in the format spec means right-align; the string is padded with spaces on the left to fill a width-10 field.
In C++20, `std::jthread` differs from `std::thread` primarily because:
Answer: It automatically joins on destruction and supports cooperative cancellation via stop_token
std::jthread's destructor calls request_stop() then join(), avoiding the terminate() that std::thread calls if destroyed while joinable.
What is the C++20 `std::bit_cast(from)` used for?
Answer: Reinterpreting the bit pattern of from as type To without undefined behavior
std::bit_cast provides a well-defined way to reinterpret the raw bytes of one type as another, replacing the undefined-behavior memcpy hack.
In C++20, `std::ranges::sort(v)` differs from `std::sort(v.begin(), v.end())` in that:
Answer: It accepts the range directly and supports projections via an extra parameter
Ranges algorithms take range objects directly and optionally accept a projection to transform elements before comparison, reducing boilerplate.