Using import over #include means getting rid of the preprocessor |
Using modules doesn't get rid of the preprocessor, their use simply changes how the preprocessor does its work.
https://andreasfertig.blog/2021/09/cpp20-modules-the-possible-speedup/
With modules there are module interface files, similar to header files, and internal partition files.
The source file where
main( ) resides is normally classified as an interface file when doing imports. At least with Visual Studio.
It sounds more complicated than it is in actual usage, though how to craft your own module code is somewhat different than writing non-module header/source files. Not "a whole new language" different, though.
One of the main advantages of modules is the speed of compiling after the initial first time needed to parse and compile the module code. As long as module code is unchanged the compiler doesn't need to recompile the module.
Think precompiled headers, the C++ stdlib libraries only need to be compiled once with each project. User created modules might if the code within the file(s) changes.
If the Arduino compilers aren't at least C++20 compliant then importing modules won't work. Even main-stream compilers other than MSVC currently have only partial support for modules (as well as a handful of other features).
https://en.cppreference.com/w/cpp/compiler_support/20
C++23 modified the module importation scheme so now to use the C++ stdlib you can
import std;
or
import std.compat;
.
https://open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2465r1.pdf
Before C++23 was finalized MS had their own version,
import std.core;, but it was buggy as all get out.
I have a few very rudimentary examples repo'd on using modules. Pre-C++20, C++20 and C++23.
Rude, crude and lewd, but it lets someone get the basic idea of modules. Along with formatting output available starting with C++20.
https://github.com/GeorgePimpleton/module_testing
+-----+
FYI, if you like the C++ formatting options but want/need to avoid C++20/23 there's the
{fmt} library that requires only C++11. A minimal subset of C++11.
https://fmt.dev/latest/