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 is the primary advantage of `std::string_view` over `const std::string&` as a function parameter?
Answer: Avoids copying and works with any contiguous char sequence
std::string_view is a non-owning reference to any contiguous char sequence, avoiding the potential heap allocation that const string& may trigger from C-strings.
Which C++17 type holds exactly one value from a fixed set of types and throws `std::bad_variant_access` on wrong access?
Answer: std::variant
std::variant is a type-safe union that holds one of its listed types and throws on incorrect type access via std::get.
What does `std::any` store differently compared to `std::variant`?
Answer: An arbitrary type not known at compile time
std::any can hold any CopyConstructible type decided at runtime, whereas std::variant requires the type set to be fixed at compile time.
Which C++17 parallel execution policy requests that the algorithm execute with vectorization and multithreading?
Answer: std::execution::par_unseq
par_unseq permits both parallel and vectorized (SIMD) execution, imposing the most restrictions on the user's callable.
What does the C++17 `std::filesystem::path` operator `/` do?
Answer: Appends a path component using the platform separator
operator/ on filesystem::path concatenates path components, inserting the platform's preferred separator between them.
In C++17, `std::invoke(f, args...)` is preferred over `f(args...)` primarily because:
Answer: It uniformly handles free functions, member functions, and member pointers
std::invoke provides a uniform call syntax that works for callable objects, member function pointers with an object, and member data pointers.
What is the purpose of `std::as_const(x)` introduced in C++17?
Answer: Returns a const reference to x without making a copy
std::as_const returns const T& for a given T& argument, enabling const overload selection without a static_cast.