What are C++20 features?
Why Interviewers Ask This
Senior C++ engineers are expected to reason about architecture, performance, and edge cases. This question separates mid-level from senior candidates by testing deep system-level understanding.
Answer
C++20 is one of the largest updates to the language. Key features: 1. Concepts: constrain template parameters with readable requirements: template<typename T> concept Numeric = std::integral<T> || std::floating_point<T>; template<Numeric T> T sum(T a, T b) { return a + b; } // Requires clause: template<typename T> requires requires(T t) { t.serialize(); t.size(); } void process(T obj) { obj.serialize(); }; 2. Ranges: composable, lazy range algorithms: #include <ranges> std::vector<int> v = {1,2,3,4,5,6,7,8,9,10}; auto result = v | std::views::filter([](int x){ return x % 2 == 0; }) | std::views::transform([](int x){ return x * x; }) | std::views::take(3); // Lazy -- no intermediate containers for (int x : result) std::cout << x << " "; // 4 16 36; 3. Coroutines: suspendable functions: cppcoro::generator<int> fibonacci() { int a = 0, b = 1; while (true) { co_yield a; std::tie(a, b) = std::make_tuple(b, a + b); } }; 4. Modules: replace header files with faster, cleaner module system: // math.cppm export module math; export int add(int a, int b) { return a + b; } // main.cpp import math; int x = add(3, 4);; 5. std::span: non-owning view over contiguous memory: void process(std::span<const int> data) { for (int x : data) std::cout << x; } process(std::vector<int>{1,2,3}); // Works process(std::array<int,3>{1,2,3}); // Works; 6. std::format: type-safe printf-like formatting: std::string s = std::format("Hello {}, you are {} years old", name, age); std::format("{:.2f}", 3.14159); // "3.14"; 7. Three-way comparison (<=> spaceship operator): auto operator<=>(const MyClass&) const = default; // All 6 comparisons!; 8. consteval, constinit; 9. Abbreviated function templates: auto square(auto x) { return x * x; }; 10. std::jthread: joinable thread that auto-joins on destruction + supports stop tokens.
Pro Tip
Before answering, structure your response: one-line definition → real-world analogy → concrete example from a project. This makes even complex C++ answers easy to follow.