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
In C++20 Concepts, what does `template requires std::integral` enforce?
Answer: T must satisfy the std::integral concept at compile time
The requires clause constrains the template, producing a clear diagnostic if the type does not satisfy std::integral.
Which C++20 feature allows `auto add(auto a, auto b)` as shorthand for a function template?
Answer: Abbreviated function templates
C++20 abbreviated function templates use auto in parameter position to implicitly declare a template parameter for each auto.
What keyword makes a C++20 function a coroutine when it appears in the function body?
Answer: co_await, co_yield, or co_return
Any function containing co_await, co_yield, or co_return is a coroutine in C++20; no special return-type keyword is needed.
What does the C++20 Ranges `std::views::filter` return?
Answer: A lazy view that skips elements not satisfying the predicate
std::views::filter returns a range adaptor view that lazily filters elements without allocating a new container.
In C++20, `std::span` differs from `int*` in that:
Answer: It carries both a pointer and a size, providing bounds information
std::span is a non-owning view over a contiguous sequence, pairing a pointer with an extent (size) for safe access.
What is the purpose of the C++20 `[[likely]]` and `[[unlikely]]` attributes?
Answer: Hint to the compiler about branch prediction probabilities
These attributes advise the compiler which branch is taken more often, potentially improving generated code layout for performance.
In C++20 modules, what is the effect of `export module mymodule;` at the top of a file?
Answer: Declares the file as the module interface unit for mymodule
export module mymodule; declares the file as mymodule's primary module interface unit, where exported declarations become visible to importers.