Overview of C++20 New Features and Frequently Tested Topics
This article provides a comprehensive overview of C++20's major new features—including concepts, modules, coroutines, ranges, three‑way comparison, constexpr enhancements, and the updated standard library—along with practical code examples, explanations of their significance, and frequently examined interview topics.
C++20 New Features
C++17, C++20 and other recent versions are new standards of the C++ language. Each new standard introduces syntax, libraries and language improvements to meet modern development needs and provide a better programming experience.
C++20, the sixth C++ standard released in 2020, adds concepts, three‑way comparison, coroutines, ranges, modules, a date‑time library, digit separators and many other enhancements. Existing features such as constexpr , template metaprogramming, tuples and smart pointers are also refined.
New standards aim to improve syntax, increase developer productivity, enhance code readability, reduce bugs and deliver better performance. Using the new standards requires a compiler that supports them and appropriate compilation flags.
1.1 Importance of C++20
Although C++20 has been out for over a year, few compilers fully support all its features, reflecting the magnitude of the changes. The new features can simplify code and fundamentally change how we organize source files.
1.2 Four Major Modules
The four major modules introduced by C++20 are Modules, Concepts, Ranges and Coroutines.
Modules
Modules completely change source‑file organization; separate .h and .cpp files are no longer required.
Example:
import iostream
export module test_m
export void test_Func(){
std::cout << "Test from C++20\n";
}Main program:
import test_m
int main(int argc, char** argv){
test_Func();
return 0;
}Ranges
Ranges provide a new way to compose algorithms, offering lazy evaluation and eliminating intermediate containers.
Traditional pipeline:
// before C++20
vector data {1,2,3,4,5,6,7,8,9,10};
auto result {
transform(reverse(drop(transform(filter(data,isOdd),doubleNum),2)),to_string)
};Using the pipe operator:
vector data {1,2,3,4,5,6,7,8,9,10};
auto result = data
| views::filter([](const auto& v){ return v % 2 == 0; })
| views::transform([](const auto& v){ return v * 2.0; })
| views::drop(2)
| views::reverse
| views::transform([](int i){ return std::to_string(i); });Concepts
Concepts allow static constraints on template parameters, making template code easier to write and understand.
Defining a concept:
template<typename T>
concept test_Concept = requires(T x){
x -= 1;
};Using the concept to constrain a function:
// pre‑C++20 style
template<typename T> requires test_Concept<T>
void Func(T t);
// post‑C++20 style
template<typename T>
void Func(T t) requires test_Concept<T>;Coroutines
Coroutines are functions that can suspend and resume, enabling asynchronous code to be written in a synchronous style.
Key keywords are co_await , co_yield and co_return . The standard library provides std::coroutine_traits and std::coroutine_handle to customize coroutine behavior.
1.3 Lambda Updates
Lambda capture of this now requires explicit capture ( [=, this] ). C++20 also supports template lambdas, allowing generic lambdas with explicit template parameters.
[]1.4 Attribute Enhancements
New attributes such as [[likely]] , [[unlikely]] , [[nodiscard(reason)]] , and the calendar/timezone library improve optimization hints and diagnostics.
switch (value) {
case 1: break;
[[likely]] case 2: break;
[[unlikely]] case 3: break;
}1.5 constexpr and consteval
C++20 relaxes many constexpr restrictions, allowing constexpr on virtual functions, lambdas, and even on std::string and std::vector . The new consteval specifier forces compile‑time evaluation.
1.6 std::format and Bit Operations
The std::format library provides type‑safe, Python‑like string formatting. New bit‑manipulation utilities ( rotl , rotr , has_single_bit , etc.) and shift operations enhance low‑level programming.
1.7 Additional Library Additions
New containers such as std::span , extended std::chrono calendars, timezone clocks, and design‑ated initializers simplify common patterns.
Frequently Tested C++20 Topics
Typical interview questions cover concepts, modules, coroutines, ranges, three‑way comparison, lambda improvements, constexpr evolution, and the std::format API.
Sample Interview List
Concepts – template constraints.
Three‑way comparison operator <=> .
Init‑capture in lambdas.
Coroutines and related keywords.
Modules and import/export syntax.
Ranges library usage.
Formatted output with std::format .
Digit separators for readability.
These topics reflect the most important changes introduced by C++20 and are commonly examined in technical interviews.
Note: The article also contains promotional material for a Linux C++ study group and related resources, which does not affect the technical content.
Deepin Linux
Research areas: Windows & Linux platforms, C/C++ backend development, embedded systems and Linux kernel, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.