Skip to content

[C++20] [Modules] [std module] Implicitly import std if standard library header is included #138906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
apolukhin opened this issue May 7, 2025 · 3 comments
Labels
clang:modules C++20 modules and Clang Header Modules libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Comments

@apolukhin
Copy link
Member

What do you think about the following idea: add a preprocessor macro that replaces content of the header with import std; if the macro is defined by user.

For example libcxx/include/vector can be changed in the following way:

#ifndef _LIBCPP_VECTOR
#define _LIBCPP_VECTOR

#if defined(LIBCPP_IMPLICIT_MODULES_USE)
import std;
#  include <version>
#else

...  // all the remaining code from <vector>

#endif // LIBCPP_IMPLICIT_MODULES_USE

#endif // _LIBCPP_VECTOR

Motivation:

  1. Implicit import std; in standard library header would improve compile times if module std is available.
  2. Migration from includes to import std; for big projects is not trivial. The above approach eases that migration
  3. It's a possible alternative solution to [C++23] [Modules] [std module] Skip including standard headers if the std module is imported #80663
@EugeneZelenko EugeneZelenko added clang:modules C++20 modules and Clang Header Modules and removed new issue labels May 7, 2025
@llvmbot
Copy link
Member

llvmbot commented May 7, 2025

@llvm/issue-subscribers-clang-modules

Author: Antony Polukhin (apolukhin)

What do you think about the following idea: add a preprocessor macro that replaces content of the header with `import std;` if the macro is defined by user.

For example libcxx/include/vector can be changed in the following way:

#ifndef _LIBCPP_VECTOR
#define _LIBCPP_VECTOR

#if defined(LIBCPP_IMPLICIT_MODULES_USE)
import std;
#  include &lt;version&gt;
#else

...  // all the remaining code from &lt;vector&gt;

#endif // LIBCPP_IMPLICIT_MODULES_USE

#endif // _LIBCPP_VECTOR

Motivation:

  1. Implicit import std; in standard library header would improve compile times if module std is available.
  2. Migration from includes to import std; for big projects is not trivial. The above approach eases that migration
  3. It's a possible alternative solution to [C++23] [Modules] [std module] Skip including standard headers if the std module is imported #80663

@ChuanqiXu9
Copy link
Member

If I read correctly, this is an issue to libc++ instead of clang? I'll try to add a tag to libc++.

For the issue itself, I feel good.

@ChuanqiXu9 ChuanqiXu9 added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label May 8, 2025
@philnik777
Copy link
Contributor

I'm not particularly enthusiastic about this. AFAIK you can achieve the same thing by using Clang modules, which work today without any additional changes to libc++.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:modules C++20 modules and Clang Header Modules libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

No branches or pull requests

5 participants