问题
In C++20 it is possible to instead of include headers with
#include "header.h"
use
#import "header.h" // Edit: this is not standard see comments
(this is for old code that cannot fully be converted to modules and use the normal import keyword without #)
My question is about preprocessing. For a long time it as only been possible to precompile and use one header per translation unit. (Clang seems to have some special case with cascading include files, that I do not consider here)
Will it be possible to precompile and use multiple header files per translation unit with the #import
keyword, now that the header files can be somewhat isolated?
Edit:
So my misunderstanding of this seemed to be from the fact that, when enabling modules in for example g++, using #include "..."
turns into import ""
automatically (I think).
I found this video Nathan Sidwell “Converting to C++20 Modules” where Nathan explains how to precompile "header units", in what I guess is the modules branch in g++, but it does not seem to work in clang++-10. I don't know if this is something that will work on other compilers as well.
回答1:
Yes, it is possible to import
any number of header units into a translation unit. Because the rules for modules prevent any interaction between them (in particular, they cannot share macros), they can all be “precompiled” independently. That said, some implementations may copy certain components from the resulting compiled module interface files into the translation unit being compiled to better emulate the behavior of textual inclusion (especially in combination with questionable declarations in the header like those with internal linkage).
Separately, #include
applied to a header file which the implementation knows (perhaps from some option or configuration file) to be usable as a header unit may be reinterpreted as import
to provide this behavior even for unmodified files (in particular, for header files from another library that you don’t control).
来源:https://stackoverflow.com/questions/64124842/c20-import-statement-will-it-be-possible-to-use-multiple-preprocessed-hea