For solving a code-golf challenge, I want to produce the smallest possible code. I had the idea of defining a shorthand for import:
#define I import
I<vector>;
Of course, the intention here is to reuse I to actually save bytes.
Is this legal in C++20?
Thoughts / What I found out so far:
- According to cppreference, "The module and import directives are also preprocessing directives". So I think it would boil down to the question whether we have a guarantee that the preprocessor first has to replace I with our definition?
- I think handling the
importdirective should happen in translation phase 4, and for the whole phase,Ishould not be macro-expanded unless specified otherwise ([cpp.pre]-7). Is it specified otherwise for this case? - Is it possible this works as part of the preprocessor rescan?
- Clang and GCC on godbolt do not compile, but AFAIK they don't yet support importing standard library headers without extra steps, and they give the same error message with the shorthand version, which indicates it would work(?)
- The same approach, but with
includeinstead ofimport, does not work with gcc and clang and thus probably isn't legal.