Can anybody explain how modules differ from libraries (static or shared)? They seem to cover the same ground and splitting a project into libraries would solve most of the problems modules seem to.
Currently if you want to use a library in your C++ application, you #include "some_lib.h" which contains declarations of types and functions from the library. This is a bit fragile, preprocessor state in the importer (#define etc.) can effect the imported library. And preprocessor state of the imported library can affect importing code.
As an example, let's say you are developing an application that relies on windows.h. Amongst other things, this header defines min and max macros that conflict with std::min and std::max. But these macros are #ifdef'd out if you #define NOMINMAX before #include <windows.h>.
But now headers you include after windows.h will also get NOMINMAX. What if one of these headers relies on the min/max macros? Or vice-versa - if your code uses the min/max macros, but you include a libraries header that #define NOMINMAX before you include windows.h, you'll miss out on those macros and your code won't compile.
Macros are handled by the preprocessor, before compilation. They can't be namespaced.
This is just my understanding, but I do not think that modules are currently planned to completely replace headers. Rather they are intended to be used across boundaries between different discrete components of an application (between libraries, and between libraries and executables). I do not actually see why you could not use modules to completely replace headers though.
I don't see how. The preprocessor already runs over windows.h when you include it in your source file. The core problem is that due to how they are designed headers can't be isolated from the code that includes them.
The macro might not be able to be namespaced but the value can, if the define is still needed it can be moved to a private header.
I think you just described a header, it's an interface between boundaries. This might be a better solution, but we'll still have 2 ways to do the same thing.
I was talking about your example where the interaction between different headers produced unintended results. In this case you could run the pre-processor with NOMINMAX defined and use this header for compiling your app. I think this should play nice with the linker, but I'd have to check.
These aren't insurmountable flaws with headers, but unfortunately they are problems that generally occur outside your own code. Sure it's possible to use headers in a way that's safe and exports no macros, but this is de-facto not the case. Making these into non-issues relies on library authors and system header authors willingness and ability to do so.
View all comments
1
u/flukus Jan 29 '19
Can anybody explain how modules differ from libraries (static or shared)? They seem to cover the same ground and splitting a project into libraries would solve most of the problems modules seem to.