Skip to content

Implement initial version of C++20 module boost.type_index#15

Merged
apolukhin merged 11 commits intoapolukhin:developfrom
boostorg:modules
May 12, 2025
Merged

Implement initial version of C++20 module boost.type_index#15
apolukhin merged 11 commits intoapolukhin:developfrom
boostorg:modules

Conversation

@apolukhin
Copy link
Copy Markdown
Owner

#include <boost/type_index... is now implicitly does import boost.type_index if the modules are supported
All the library internals now have unconditional module level linkage.

Significant differences from https://anarthal.github.io/cppblog/modules3:

  • BOOST_TYPE_INDEX_USE_STD_MODULE macro switch for import std; / includes while building module. This allows to use module in C++20 and even without usable std module.

@apolukhin
Copy link
Copy Markdown
Owner Author

@anarthal I'd appreciate comments on this PR on modules (as you proposed at boostorg/core#191 (comment))

BTW, does the MSVC with import std; actually work in at least one of the prototype implementations from https://anarthal.github.io/cppblog/modules3 ?

@anarthal
Copy link
Copy Markdown

AFAIR I made it work at some point. Subsequent changes may have broken it again though.

Comment thread CMakeLists.txt Outdated
add_library(boost_type_index)
target_sources(boost_type_index PUBLIC
FILE_SET modules_public TYPE CXX_MODULES FILES
${CMAKE_CURRENT_LIST_DIR}/modules/type_index.cppm
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convention is boost_type_index.cppm

Comment thread modules/type_index.cppm Outdated
#include <boost/throw_exception.hpp>

#ifdef BOOST_TYPE_INDEX_USE_STD_MODULE
import std;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is technically not allowed. A global module fragment can only contain preprocessor directives, so you should place this in an include. AFAIR clang doesn't complain and MSVC issues a warning. The change I'm suggesting doesn't imply any change in behavior.

Comment thread modules/type_index.cppm
# pragma clang diagnostic ignored "-Winclude-angled-in-module-purview"
#endif

#include <boost/type_index.hpp>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you include your library files in the purview, you need to be extra careful with if-defing out all third party includes. I've seen a <boost/config.hpp> without an ifdef, which would cause any names defined by it (including the ones poured by including STL headers) into the named module.

Comment thread modules/type_index.cppm

export module boost.type_index;

#ifdef __clang__
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MSVC has a similar warning

Comment thread include/boost/type_index.hpp Outdated

#include <boost/type_index/detail/config.hpp>

#include <boost/config.hpp>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're including this in the purview, you need to ifdef this include out.

#include <type_traits>
#endif

#include <boost/throw_exception.hpp>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to ifdef-out other Boost headers, too.


#if !defined(BOOST_USE_MODULES) || defined(BOOST_TYPE_INDEX_INTERFACE_UNIT)

#include <boost/config.hpp>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to ifdef-out other Boost headers, too.

#if !defined(BOOST_USE_MODULES) || defined(BOOST_TYPE_INDEX_INTERFACE_UNIT)

#include <boost/type_index/runtime_cast/detail/runtime_cast_impl.hpp>
#include <boost/throw_exception.hpp>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Boost header

Comment thread modules/type_index.cppm
module;

#include <version>
#include <cstddef>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work under MSVC? Last time I checked, mixing #include <cstddef> and import std failed. If you encounter this problem, #include <stddef.h> doesn't cause this problem.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fail to workaround too many issues on MSVC, so I'd say that "boost.any module does not support MSVC for now".

Comment thread CMakeLists.txt
add_library(boost_type_index INTERFACE)
set(__scope INTERFACE)
endif()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I missing something or is there no ${CMAKE_CURRENT_SOURCE_DIR}/test/CMakeLists.txt? I'd advise to remove this check if you know for sure that the file should be present. AFAIK this is generated by Peter's CMake generator so it's compatible with libraries that don't run their tests via CMake.

@apolukhin apolukhin merged commit dc78cf1 into apolukhin:develop May 12, 2025
13 of 14 checks passed
@apolukhin apolukhin deleted the modules branch May 12, 2025 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants