
Adventures in C++
My experiences in learning C++

C++20 and Modules
there is basic modules support since gcc-11 (when using the -fmodules-ts compiler option like:
g++ -fmodules-ts -std=c++20 helloworld.cpp -o helloworld
however this will fail since the module for <print> is not compiled yet.
g++ -fmodules-ts -std=c++20 -c -x c++-system-header print
This should generate a directory called gcm.cache and buried deep in that folder structure will be the print.gcm file (but you can leave it there. The compiler knows where to look.
if you compile a module you've written yourself it will be placed in gcm.cache as-well.
Very basic Example using C++20 modules

The module interface file several sources use the file extension cppm (clang) or ixx (Visual Studio), the gcc compiler (at this moment) does not support these extensions because they feel there is no need for another set of file extension. to distinguish between the interface and implementation files I use the file extension cxx for the module interface file and just cpp for the implementation.

The module implementation file contains the actual code.

the main source file imports the required module and uses it's sayHello function.
These files need to be compiled in the correct order to avoid errors during compilation, My workflow is as follows
- /opt/gcc-15/bin/g++ -fmodules-ts -std=c++23 -c -x c++-system-header -static-libstdc++ print
- /opt/gcc-15/bin/g++ -fmodules-ts -std=c++23 -c -static-libstdc++ hello.cxx
- /opt/gcc-15/bin/g++ -fmodules-ts -std=c++23 -static-libstdc++ hello.cxx hello.cpp main.cpp -o helloworld
In this example I use -std=c++23 and not -std=C++20, the reason for this is that print is unknown (lat least at this moment) by the std library. If you want to use c++20 you can import <iostream>.
The spaceship operator
The three-way-comparison a.k.a. the Spaceship operator ( <=> ) is another new thing in the language since C++20. It basically does the three size comparison operations in one. This is especially useful in situations where doing the comparisons separately is resource heavy.
import <compare>;
import <print>;
int main() {
a simple example:
int value{42};
std::strong_ordering result { value <=> 19 }
std::println("value < 19: {}", result == std::strong_ordering::less};
std::println("value < 19: {}",result == std::strong_ordering::greater);
std::println("value = 19: {}",result == std::strong_ordering::equal);
return 0;
}

C++23 and Modules
(At the moment of posting, this is not yet possible with my compiler [a snapshot of the most recent GCC-15] so at the moment we will still need to import the modules separately..
About
The most common sources are:
Beginning C++20 (978-1-4842-5884-2)
- Ivor Horton
- Peter van Weert
Professional C++ 6th Edition (978-1-394-19317-2)
- Marc Gregoire