Modules and Coroutines
Modules and Coroutines
A fast reference to two major C++20 features that need deliberate adoption.
Modules and Coroutines
A fast reference to two major C++20 features that need deliberate adoption.
export module math;
export int add(int a, int b) {
return a + b;
}
import math;
export module math;
export int add(int a, int b);
module math;
int add(int a, int b) {
return a + b;
}
Keep the interface unit small and stable. Move implementation details out of the exported surface when possible.
add_library(math)
target_sources(math
PUBLIC
FILE_SET CXX_MODULES FILES math.cppm
PRIVATE
math.cpp
)
The key reminder is that module adoption is a build-model change, not just a syntax change.
generator<int> countdown(int from) {
while (from > 0) {
co_yield from--;
}
}
co_return finishes with a value.co_yield produces a sequence element.co_await suspends until another awaitable is ready.task<int> compute() {
int base = co_await read_value_async();
co_return base * 2;
}
Even this tiny example shows the real coroutine value: the code reads top-to-bottom while still modeling suspension.
// math.cppm
export module math;
export int add(int a, int b);
// main.cpp
import math;
int main() { return add(2, 3); }
Swap one older pattern for a newer standard facility, then compare clarity. This keeps modern C++ grounded in practical tradeoffs instead of feature tourism.
#include <format>
#include <string>
int main() {
std::string line = std::format("{} items ready", 3);
return static_cast<int>(line.size());
}