CPP Modern C++ Features (C++17/20) 3 — Questions and Answers
Question 1: What is the primary advantage of `std::string_view` over `const std::string&` as a function parameter?
- Avoids copying and works with any contiguous char sequence (Correct answer)
- Provides null-termination guarantees
- Owns its character data
- Supports Unicode by default
Correct 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.
Question 2: Which C++17 type holds exactly one value from a fixed set of types and throws `std::bad_variant_access` on wrong access?
- std::variant (Correct answer)
- std::any
- std::optional
- std::tuple
Correct 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.
Question 3: What does `std::any` store differently compared to `std::variant`?
- An arbitrary type not known at compile time (Correct answer)
- Only trivially copyable types
- A fixed list of types
- Only pointer types
Correct 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.
Question 4: Which C++17 parallel execution policy requests that the algorithm execute with vectorization and multithreading?
- std::execution::par_unseq (Correct answer)
- std::execution::seq
- std::execution::par
- std::execution::vec
Correct answer: std::execution::par_unseq
par_unseq permits both parallel and vectorized (SIMD) execution, imposing the most restrictions on the user's callable.
Question 5: What does the C++17 `std::filesystem::path` operator `/` do?
- Appends a path component using the platform separator (Correct answer)
- Divides two integers in the path
- Strips a directory level
- Normalizes the path
Correct answer: Appends a path component using the platform separator
operator/ on filesystem::path concatenates path components, inserting the platform's preferred separator between them.
Question 6: In C++17, `std::invoke(f, args...)` is preferred over `f(args...)` primarily because:
- It uniformly handles free functions, member functions, and member pointers (Correct answer)
- It is faster than direct calls
- It supports only lambdas
- It is required for noexcept functions
Correct 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.
Question 7: What is the purpose of `std::as_const(x)` introduced in C++17?
- Returns a const reference to x without making a copy (Correct answer)
- Casts x to a const value type
- Marks x as constexpr
- Creates a const copy of x
Correct 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.
What is the primary advantage of `std::string_view` over `const std::string&` as a function parameter?