c++moduleprecompiled-headers

c++ precompiled headers vs. modules


I'm confused on the difference between precompiled headers and modules. What advantage does one have over the other? I've read the Microsoft documentation on both of them but it hasn't helped me much.


Solution

  • An advantage of modules is that they are a standard feature. All C++20 compilers must implement them as described in the language. Precompiled headers are not a standard feature. Not all compilers necessarily have that feature, and each compiler that has, implements them in their own way that isn't necessarily compatible with other compilers.

    An advantage of modules over headers is that they encapsulate macros. A (rarely useful) advantage of headers is that they can "export" macros.

    An advantage of modules over headers is that you have explicit control over what names you export, which allows encapsulating implementation details.

    At the moment of writing, a disadvantage of modules is that only MSVC has fully implemented them so far.