0

There's a way to conditionaly use import statements with the C++20 modules feature?

// Pseudocode

IF OS == WINDOWS:

import std.io;

ELSE:

import <iostream>;
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
Alex Vergara
  • 1,766
  • 1
  • 10
  • 29

1 Answers1

4

You use macros, just like you would for most other conditional compilation operations. And yes, this means that modules have to be built differently for different command line options. But that was always going to be the case.

Also FYI: std.io is a thing provided by MSVC, not Windows. And you should avoid using it due to its non-standard (and likely not-going-to-be-standard) status. If you must use one of MSVC's standard library modules, use import std;.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • You are right as always @NicolBolas. The fact it's that I have a Clang C++20 project, that usually I build in Unix systems. Using the Clang common command line options, I build without issues using import statements instead include directives. But sometimes I am working on Windows, and don't want to switch back to Arch, so I code, but using #include directives, I always get `error: reference to 'type_info' is ambiguous const type_info& _Target_type() const noexcept override {...` and if I use import statements, I get `cannot be imported because it is not known to be a header unit` – Alex Vergara Jun 09 '22 at 17:59
  • So I was wondering if I can use a preprocessor directive to replace them, but #include directives wasn't working at this moment for me. By the way, build process is completly automated with this tool `https://github.com/Pyzyryab/Zork`, so I must include different options to get those header working correctly under Windows, but I am still trying the correct Clang cmd options to make it work (no success up until this point) – Alex Vergara Jun 09 '22 at 18:01