Boost logo

Boost :

From: Antony Polukhin (antoshkka_at_[hidden])
Date: 2024-07-06 12:20:53


Hi,

C++20 modules seem to be a good tool to improve compile times and to hide
the implementation details of the libraries.

I've been looking throught the sources of {fmt} library. Looks like the fmt
library approach to C++20 modularization seems to fit Boost quite well.

How about adding the following macro to Boost:
* BOOST_BEGIN_MODULE_EXPORT - starts the scope of things to export if Boost
is build as a C++20 module
* BOOST_END_MODULE_EXPORT - ends the scope of things to export if Boost is
build as a C++20 module
* BOOST_MODULE_EXPORT - for a single entity export if Boost is build as a
C++20 module

As a result, if Boost is build as a module the above macros are defined in
the following way:

#define BOOST_MODULE_EXPORT export
#define BOOST_BEGIN_MODULE_EXPORT export {
#define BOOST_END_MODULE_EXPORT }

Helper macro BOOST_NO_CXX23_IMPORT_STD for detecting availability of
`import std` also seems useful.

Macro BOOST_NO_CXX20_MODULES - for notifying the libraries that compiler
does not support modules or Boost modules are not compiled.

With all the above macros the Boost libraries could look like:

/// header

#include <boost/library_that_is_known_to_not_support_modules.hpp>

#ifdef BOOST_NO_CXX20_MODULES
#include <boost/library_that_supports_modules.hpp>
#else
import Boost.LibraryThatSupportsModules;
#endif

#ifdef BOOST_NO_CXX23_IMPORT_STD
#include <iostream>
#else
import std;
#endif

namespace boost {
namespace detail {
template <class T>
bool try_do(const T&);
}

BOOST_BEGIN_MODULE_EXPORT

template <class T, class From>
bool try_lexiacal_convert(const From& f, T& to) { /*impl*/ }

template <class T, class From>
T lexiacal_cast(const From& f) { /*impl*/ }

BOOST_END_MODULE_EXPORT
}

/// module src file

export module Boost.MyLibraryName;

#ifdef BOOST_NO_CXX20_MODULES
#error ???
#endif

#include <boost/my_library.hpp>

-- 
Best regards,
Antony Polukhin

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk