← Back to Pull requests

Initial support for the C++20 modules #191

Open
apolukhin opened this pull request with 782 lines diff across 53 files
View on GitHub
apolukhin apolukhin commented on Apr 03, 2025 17:35 UTC
Author

The PR follows an experimental modules approach from Boost.PFR https://github.com/boostorg/pfr/commit/60391652fa92b7a237583f00c863caa78ab8a0f1

The approach is an experiment. Any comments, suggestions are welcome!

Diff: 782 lines (+782 / -0) across 53 files Reviewer: Lastique Review duration: 839265m
All Vouch review confidence checks passed
Residual risk 0.08 = Change Risk (0.78) × (1 − Review Confidence (0.89)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.78 · 782 lines diff across 53 files
0.78
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.91 · 5 substantive comment(s)
0.91
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
89%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.399
Mass File Touch mass_file_touch
0.080
Real PR Conversation Timeline
18 items
Lastique Lastique commented on code on Apr 03, 2025 20:48 UTC
Code Review
include/boost/core/detail/is_same.hpp:1
@@ -22,6 +24,8 @@ namespace core
 namespace detail
 {
 
+BOOST_CORE_BEGIN_MODULE_EXPORT

I don't think this should be exported. core::detail::is_same is an implementation detail and should not be used by users.

Lastique Lastique commented on code on Apr 03, 2025 20:49 UTC
Code Review
module/core.cppm:1
@@ -0,0 +1,207 @@
+// Copyright (c) 2025 Antony Polukhin
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// To compile manually use a command like the folowing:
+// clang++ -I ../include -std=c++20 --precompile -x c++-module core.cppm
+
+#define BOOST_CORE_BEGIN_MODULE_EXPORT export {
+#define BOOST_CORE_END_MODULE_EXPORT }
+
+module;
+
+#include <boost/config/header_deprecated.hpp>
+#include <boost/config.hpp>
+#include <boost/config/no_tr1/memory.hpp>
+#include <boost/config/pragma_message.hpp>
+#include <boost/config/workaround.hpp>
+
+#include <boost/assert.hpp>
+#include <boost/assert/source_location.hpp>
+
+#include <boost/current_function.hpp>
+#include <boost/static_assert.hpp>
+
+#include <boost/throw_exception.hpp>  // TODO: modularize
+
+#if (defined(__cpp_lib_uncaught_exceptions) && __cpp_lib_uncaught_exceptions >= 201411)
+#if defined(__APPLE__)
+#include <Availability.h>
+#endif
+#endif
+
+#ifndef BOOST_CORE_HAS_STD_MODULE
+#include <algorithm>
+#include <array>
+#include <cctype>
+#include <climits>
+#include <cmath>
+#include <cstdarg>
+#include <cstddef>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <cxxabi.h>
+#include <exception>
+#include <functional>
+#include <initializer_list>
+#include <ios>
+#include <iosfwd>
+#include <iostream>
+#include <iterator>
+#include <limits>
+#include <memory>
+#include <new>
+#include <stdexcept>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string>
+#include <time.h>
+#include <typeinfo>
+#include <type_traits>
+#include <utility>
+#include <wchar.h>

Why are all these includes needed? Everything needed by Boost.Core should be included by Boost.Core headers.

Lastique Lastique commented on code on Apr 03, 2025 20:50 UTC
Code Review
module/core.cppm:1
@@ -0,0 +1,207 @@
+// Copyright (c) 2025 Antony Polukhin
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// To compile manually use a command like the folowing:
+// clang++ -I ../include -std=c++20 --precompile -x c++-module core.cppm
+
+#define BOOST_CORE_BEGIN_MODULE_EXPORT export {
+#define BOOST_CORE_END_MODULE_EXPORT }
+
+module;
+
+#include <boost/config/header_deprecated.hpp>
+#include <boost/config.hpp>
+#include <boost/config/no_tr1/memory.hpp>
+#include <boost/config/pragma_message.hpp>
+#include <boost/config/workaround.hpp>
+
+#include <boost/assert.hpp>
+#include <boost/assert/source_location.hpp>
+
+#include <boost/current_function.hpp>
+#include <boost/static_assert.hpp>
+
+#include <boost/throw_exception.hpp>  // TODO: modularize
+
+#if (defined(__cpp_lib_uncaught_exceptions) && __cpp_lib_uncaught_exceptions >= 201411)
+#if defined(__APPLE__)
+#include <Availability.h>
+#endif
+#endif
+
+#ifndef BOOST_CORE_HAS_STD_MODULE
+#include <algorithm>
+#include <array>
+#include <cctype>
+#include <climits>
+#include <cmath>
+#include <cstdarg>
+#include <cstddef>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <cxxabi.h>
+#include <exception>
+#include <functional>
+#include <initializer_list>
+#include <ios>
+#include <iosfwd>
+#include <iostream>
+#include <iterator>
+#include <limits>
+#include <memory>
+#include <new>
+#include <stdexcept>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string>
+#include <time.h>
+#include <typeinfo>
+#include <type_traits>
+#include <utility>
+#include <wchar.h>
+#endif
+
+export module Boost.Core;
+
+#ifdef BOOST_CORE_HAS_STD_MODULE
+import std;
+#endif
+
+#ifdef __clang__
+#   pragma clang diagnostic ignored "-Winclude-angled-in-module-purview"
+#endif
+
+#ifdef BOOST_CORE_ATTACH_TO_GLOBAL_MODULE
+extern "C++" {
+#include <boost/visit_each.hpp>
+#include <boost/type.hpp>
+#include <boost/get_pointer.hpp>
+#include <boost/non_type.hpp>
+#include <boost/ref.hpp>
+#include <boost/core/bit.hpp>
+#include <boost/core/ignore_unused.hpp>
+#include <boost/core/enable_if.hpp>
+#include <boost/core/exchange.hpp>
+#include <boost/core/pointer_in_range.hpp>
+#include <boost/core/pointer_traits.hpp>
+#include <boost/core/identity.hpp>
+#include <boost/core/explicit_operator_bool.hpp>
+#include <boost/core/allocator_access.hpp>
+#include <boost/core/verbose_terminate_handler.hpp>
+#include <boost/core/type_name.hpp>
+#include <boost/core/invoke_swap.hpp>
+#include <boost/core/uncaught_exceptions.hpp>
+#include <boost/core/no_exceptions_support.hpp>
+#include <boost/core/ref.hpp>
+#include <boost/core/alloc_construct.hpp>
+#include <boost/core/first_scalar.hpp>
+#include <boost/core/lightweight_test_trait.hpp>
+#include <boost/core/checked_delete.hpp>
+#include <boost/core/alignof.hpp>
+#include <boost/core/swap.hpp>
+#include <boost/core/empty_value.hpp>
+#include <boost/core/null_deleter.hpp>
+#include <boost/core/data.hpp>
+#include <boost/core/nvp.hpp>
+#include <boost/core/snprintf.hpp>
+#include <boost/core/demangle.hpp>
+#include <boost/core/memory_resource.hpp>
+#include <boost/core/default_allocator.hpp>
+#include <boost/core/scoped_enum.hpp>
+#include <boost/core/span.hpp>
+#include <boost/core/lightweight_test.hpp>
+#include <boost/core/is_same.hpp>
+#include <boost/core/addressof.hpp>
+#include <boost/core/launder.hpp>
+#include <boost/core/noinit_adaptor.hpp>
+#include <boost/core/allocator_traits.hpp>
+#include <boost/core/serialization.hpp>
+#include <boost/core/size.hpp>
+#include <boost/core/fclose_deleter.hpp>
+#include <boost/core/typeinfo.hpp>
+#include <boost/core/quick_exit.hpp>
+#include <boost/core/max_align.hpp>
+#include <boost/core/underlying_type.hpp>
+#include <boost/core/cmath.hpp>
+#include <boost/core/noncopyable.hpp>
+#include <boost/core/yield_primitives.hpp>
+#include <boost/core/make_span.hpp>
+#include <boost/core/use_default.hpp>
+#include <boost/core/functor.hpp>
+#include <boost/checked_delete.hpp>
+#include <boost/swap.hpp>
+#include <boost/iterator.hpp>
+#include <boost/utility/enable_if.hpp>
+#include <boost/utility/explicit_operator_bool.hpp>
+#include <boost/utility/swap.hpp>
+#include <boost/utility/addressof.hpp>
+#include <boost/noncopyable.hpp>
+}
+#else
+#include <boost/visit_each.hpp>

Why the duplicate list of includes?

apolukhin apolukhin commented on code on Apr 05, 2025 14:12 UTC
Code Review
include/boost/core/detail/is_same.hpp:1
@@ -22,6 +24,8 @@ namespace core
 namespace detail
 {
 
+BOOST_CORE_BEGIN_MODULE_EXPORT

I also do not think that this should be exported.

However clang complains on exporting namespce core { using boost::core::detail::is_same; } without exporting this detail, so there's actually no choice if boost::core::is_same is expprted

apolukhin apolukhin commented on code on Apr 05, 2025 14:23 UTC
Code Review
module/core.cppm:1
@@ -0,0 +1,207 @@
+// Copyright (c) 2025 Antony Polukhin
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// To compile manually use a command like the folowing:
+// clang++ -I ../include -std=c++20 --precompile -x c++-module core.cppm
+
+#define BOOST_CORE_BEGIN_MODULE_EXPORT export {
+#define BOOST_CORE_END_MODULE_EXPORT }
+
+module;
+
+#include <boost/config/header_deprecated.hpp>
+#include <boost/config.hpp>
+#include <boost/config/no_tr1/memory.hpp>
+#include <boost/config/pragma_message.hpp>
+#include <boost/config/workaround.hpp>
+
+#include <boost/assert.hpp>
+#include <boost/assert/source_location.hpp>
+
+#include <boost/current_function.hpp>
+#include <boost/static_assert.hpp>
+
+#include <boost/throw_exception.hpp>  // TODO: modularize
+
+#if (defined(__cpp_lib_uncaught_exceptions) && __cpp_lib_uncaught_exceptions >= 201411)
+#if defined(__APPLE__)
+#include <Availability.h>
+#endif
+#endif
+
+#ifndef BOOST_CORE_HAS_STD_MODULE
+#include <algorithm>
+#include <array>
+#include <cctype>
+#include <climits>
+#include <cmath>
+#include <cstdarg>
+#include <cstddef>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <cxxabi.h>
+#include <exception>
+#include <functional>
+#include <initializer_list>
+#include <ios>
+#include <iosfwd>
+#include <iostream>
+#include <iterator>
+#include <limits>
+#include <memory>
+#include <new>
+#include <stdexcept>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string>
+#include <time.h>
+#include <typeinfo>
+#include <type_traits>
+#include <utility>
+#include <wchar.h>

That is true for headers. But when a module is created, we need to place all the standard library and OS stuff into a global module fragment to avoid same symbols being ambiguously exported from different modules of Boost.

To do so I put all the headers right after the module;, so they end up in global module fragment. Later, after the export module Boost.Core; I include all the library heders. Include guards of the standard library and OS stuff do they work, so the library headers do not actually include them again. As a result, only the library classes, functions and aliases apear after export module Boost.Core; , so the module exports only the library stuff

apolukhin apolukhin commented on code on Apr 05, 2025 14:26 UTC
Code Review
module/core.cppm:1
@@ -0,0 +1,207 @@
+// Copyright (c) 2025 Antony Polukhin
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// To compile manually use a command like the folowing:
+// clang++ -I ../include -std=c++20 --precompile -x c++-module core.cppm
+
+#define BOOST_CORE_BEGIN_MODULE_EXPORT export {
+#define BOOST_CORE_END_MODULE_EXPORT }
+
+module;
+
+#include <boost/config/header_deprecated.hpp>
+#include <boost/config.hpp>
+#include <boost/config/no_tr1/memory.hpp>
+#include <boost/config/pragma_message.hpp>
+#include <boost/config/workaround.hpp>
+
+#include <boost/assert.hpp>
+#include <boost/assert/source_location.hpp>
+
+#include <boost/current_function.hpp>
+#include <boost/static_assert.hpp>
+
+#include <boost/throw_exception.hpp>  // TODO: modularize
+
+#if (defined(__cpp_lib_uncaught_exceptions) && __cpp_lib_uncaught_exceptions >= 201411)
+#if defined(__APPLE__)
+#include <Availability.h>
+#endif
+#endif
+
+#ifndef BOOST_CORE_HAS_STD_MODULE
+#include <algorithm>
+#include <array>
+#include <cctype>
+#include <climits>
+#include <cmath>
+#include <cstdarg>
+#include <cstddef>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <cxxabi.h>
+#include <exception>
+#include <functional>
+#include <initializer_list>
+#include <ios>
+#include <iosfwd>
+#include <iostream>
+#include <iterator>
+#include <limits>
+#include <memory>
+#include <new>
+#include <stdexcept>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string>
+#include <time.h>
+#include <typeinfo>
+#include <type_traits>
+#include <utility>
+#include <wchar.h>
+#endif
+
+export module Boost.Core;
+
+#ifdef BOOST_CORE_HAS_STD_MODULE

This macro should be changed to some Boost.Config macro that is defined if the compiler understands modules and there is module std. The includes in the headers should be guarded by that macro and use import std; if it is available

Lastique Lastique commented on code on Apr 05, 2025 14:29 UTC
Code Review
include/boost/core/detail/is_same.hpp:1
@@ -22,6 +24,8 @@ namespace core
 namespace detail
 {
 
+BOOST_CORE_BEGIN_MODULE_EXPORT

That using-declaration is for backward compatibility and deprecated, it should not be exported either but rather removed.

Lastique Lastique commented on code on Apr 05, 2025 14:31 UTC
Code Review
module/core.cppm:1
@@ -0,0 +1,207 @@
+// Copyright (c) 2025 Antony Polukhin
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// To compile manually use a command like the folowing:
+// clang++ -I ../include -std=c++20 --precompile -x c++-module core.cppm
+
+#define BOOST_CORE_BEGIN_MODULE_EXPORT export {
+#define BOOST_CORE_END_MODULE_EXPORT }
+
+module;
+
+#include <boost/config/header_deprecated.hpp>
+#include <boost/config.hpp>
+#include <boost/config/no_tr1/memory.hpp>
+#include <boost/config/pragma_message.hpp>
+#include <boost/config/workaround.hpp>
+
+#include <boost/assert.hpp>
+#include <boost/assert/source_location.hpp>
+
+#include <boost/current_function.hpp>
+#include <boost/static_assert.hpp>
+
+#include <boost/throw_exception.hpp>  // TODO: modularize
+
+#if (defined(__cpp_lib_uncaught_exceptions) && __cpp_lib_uncaught_exceptions >= 201411)
+#if defined(__APPLE__)
+#include <Availability.h>
+#endif
+#endif
+
+#ifndef BOOST_CORE_HAS_STD_MODULE
+#include <algorithm>
+#include <array>
+#include <cctype>
+#include <climits>
+#include <cmath>
+#include <cstdarg>
+#include <cstddef>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <cxxabi.h>
+#include <exception>
+#include <functional>
+#include <initializer_list>
+#include <ios>
+#include <iosfwd>
+#include <iostream>
+#include <iterator>
+#include <limits>
+#include <memory>
+#include <new>
+#include <stdexcept>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string>
+#include <time.h>
+#include <typeinfo>
+#include <type_traits>
+#include <utility>
+#include <wchar.h>

If supporting modules requires duplicating includes from all headers then the whole modules undertaking is a non-starter. This is unmaintainable. If we're to support modules, this should not be required.

apolukhin apolukhin commented on code on Apr 05, 2025 18:40 UTC
Code Review
module/core.cppm:1
@@ -0,0 +1,207 @@
+// Copyright (c) 2025 Antony Polukhin
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// To compile manually use a command like the folowing:
+// clang++ -I ../include -std=c++20 --precompile -x c++-module core.cppm
+
+#define BOOST_CORE_BEGIN_MODULE_EXPORT export {
+#define BOOST_CORE_END_MODULE_EXPORT }
+
+module;
+
+#include <boost/config/header_deprecated.hpp>
+#include <boost/config.hpp>
+#include <boost/config/no_tr1/memory.hpp>
+#include <boost/config/pragma_message.hpp>
+#include <boost/config/workaround.hpp>
+
+#include <boost/assert.hpp>
+#include <boost/assert/source_location.hpp>
+
+#include <boost/current_function.hpp>
+#include <boost/static_assert.hpp>
+
+#include <boost/throw_exception.hpp>  // TODO: modularize
+
+#if (defined(__cpp_lib_uncaught_exceptions) && __cpp_lib_uncaught_exceptions >= 201411)
+#if defined(__APPLE__)
+#include <Availability.h>
+#endif
+#endif
+
+#ifndef BOOST_CORE_HAS_STD_MODULE
+#include <algorithm>
+#include <array>
+#include <cctype>
+#include <climits>
+#include <cmath>
+#include <cstdarg>
+#include <cstddef>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <cxxabi.h>
+#include <exception>
+#include <functional>
+#include <initializer_list>
+#include <ios>
+#include <iosfwd>
+#include <iostream>
+#include <iterator>
+#include <limits>
+#include <memory>
+#include <new>
+#include <stdexcept>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string>
+#include <time.h>
+#include <typeinfo>
+#include <type_traits>
+#include <utility>
+#include <wchar.h>

Now I also have a feeling that I'm doing something wrong. I'll try another approach soon

anarthal anarthal commented on Apr 05, 2025 20:22 UTC
Contributor

?

anarthal anarthal commented on Apr 05, 2025 22:44 UTC
Contributor

Oh, I misread this. @apolukhin you might have a look at the articles pointed out by Peter and the discussion we had on the ML. I will have a look at the PR on Monday.

apolukhin apolukhin commented on Apr 06, 2025 07:19 UTC
Author

@anarthal yes, I saw the links before and followed the approach from the links.

@Lastique noted that there's something wrong with such approach. Including all the standard headers before the export module seems to be an ad-hoc solution that scales poorly.

I'm planing to do thefollowin experiment with module partitions: In each header a new module partition is declared and in PMI all of them are exported

pdimov pdimov commented on Apr 06, 2025 12:44 UTC
Contributor

yes, I saw the links before and followed the approach from the links.

You also need to read the third post, which describes the approach we ultimately settled on.

anarthal anarthal commented on Apr 07, 2025 10:03 UTC
Contributor

If we want to support dual builds, I strongly recommend being able to run the library's test suite under modular builds. Otherwise, we're just shipping untested code, which I don't think we should do.

See how the two PRs mentioned in the articles have full test suites and CI builds:

https://github.com/boostorg/mp11/pull/104
https://github.com/boostorg/charconv/pull/255

apolukhin apolukhin commented on Apr 07, 2025 10:59 UTC
Author

You also need to read the third post, which describes the approach we ultimately settled on.

Oops, I was sure that saw it but now I realize the missed it. Thanks!
I see that the settled approach with export namespace is quite close to the libstdc++ approach for std module

@pdimov @anarthal I'll concentrate on C++20 modularization of libraries I maintain. Please, confirm that I understood the core principles correctly:
module should be in modules/ directory
includes should transparently for the user change into import depending on BOOST_USE_MODULES macro
module should place all the library entities into a global module fragment and use export namespace.
module build requires usable module std
all the tests should also run on the library module
module name is lowercased (for example boost.core)

anarthal anarthal commented on Apr 07, 2025 15:10 UTC
Contributor

I'd say so, yes. Also, remember to add the relevant CMake and CI code. I'm happy to review the PRs for your libraries if it helps.

Reviewers
Assignees
apolukhin apolukhin
Labels
risk: low confidence: 89.3% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 78.4% (HIGH) · Higher = larger blast radius
78.4%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 89.3% · Lower = rushed or shallow review
89.3%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 8.4% · Safe to merge
8.4%
Repository