Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Support for C++20 Modules

C++20 Modules Support

Boost.Math can be consumed as the named module boost.math. The module covers the public interface of the library: special functions, statistical distributions, constants, policies, ccmath, the public tools (root finding, minima, polynomials, rational and series evaluation, norms, condition numbers and friends), quadrature, the modern interpolators, statistics, optimization, differentiation (autodiff, finite differences and Lanczos smoothing), the complex inverse trigonometric functions, and the quaternion and octonion types.

import boost.math;

int main()
{
    return boost::math::cdf(boost::math::normal_distribution<>(), 0.0) == 0.5 ? 0 : 1;
}

Module support is completely inert unless the module is built and consumed explicitly: nothing changes for existing header users.

How to build

The module is consumable from C++20 onward. The interface unit lives in libs/math/module/math.cppm and builds with any toolchain that provides the standard library module (import std); the standard library maintainers agreed to make import std available in C++20 mode as well as C++23, so a C++23 compilation mode is not required. CI exercises two configurations, both with CMake 4.4, Ninja and the matching CMAKE_EXPERIMENTAL_CXX_IMPORT_STD UUID: clang 20 with libc++ runs the full module test suite, and GCC 15 with libstdc++ builds the module and runs the quick test (libstdc++ cannot yet mix the textual standard library includes used by the wider test sources with import std in the same translation unit). From a Boost superproject checkout:

cmake -G Ninja -DBOOST_INCLUDE_LIBRARIES=math -DBUILD_TESTING=ON \
  -DBOOST_MATH_BUILD_MODULE=ON \
  -DCMAKE_CXX_COMPILER=clang++-20 \
  -DCMAKE_CXX_FLAGS="-stdlib=libc++ -Wno-reserved-module-identifier" \
  -DCMAKE_EXPERIMENTAL_CXX_IMPORT_STD=<uuid-for-your-cmake-version> \
  -DCMAKE_CXX_STDLIB_MODULES_JSON=/usr/lib/llvm-20/lib/libc++.modules.json ..
cmake --build . --target tests
ctest

The module test suite reuses a curated subset of the regular test files, which switch from textual inclusion to import boost.math; when BOOST_MATH_BUILD_MODULE is defined. The cmake-module-test job in the library's CI runs this configuration on every commit.

Limitations

PrevUpHomeNext