Make library work with C++20 modules (#4764)

*  add test for C++20 modules

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

* 🚨 fix warning

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

* Add missing header (#4763)

* 🐛 add missing header

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

* 🚨 fix warning

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

* 🚨 fix warning

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

---------

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

---------

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann 2025-04-28 21:52:26 +02:00 committed by GitHub
parent 3b02afb9d9
commit eef76c200e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 66 additions and 5 deletions

View File

@ -143,7 +143,7 @@ jobs:
strategy:
matrix:
# older GCC docker images (4, 5, 6) fail to check out code
compiler: ['7', '8', '9', '10', '11', '12', '13', '14', 'latest']
compiler: ['7', '8', '9', '10', '11', '12', '13', '14', '15', 'latest']
container: gcc:${{ matrix.compiler }}
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
@ -221,6 +221,21 @@ jobs:
- name: Build
run: cmake --build build --target ci_cuda_example
ci_module_cpp20:
strategy:
matrix:
container: ['gcc:latest', 'silkeh/clang:latest']
runs-on: ubuntu-latest
container: ${{ matrix.container }}
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Get latest CMake and ninja
uses: lukka/get-cmake@57c20a23a6cac5b90f31864439996e5b206df9dc # v4.0.1
- name: Run CMake
run: cmake -S . -B build -DJSON_CI=On
- name: Build
run: cmake --build build --target ci_module_cpp20
ci_icpc:
runs-on: ubuntu-latest
container: ghcr.io/nlohmann/json-ci:v2.2.0

View File

@ -659,6 +659,17 @@ add_custom_target(ci_cuda_example
COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_cuda_example
)
###############################################################################
# C++ 20 modules
###############################################################################
add_custom_target(ci_module_cpp20
COMMAND ${CMAKE_COMMAND}
-DCMAKE_BUILD_TYPE=Debug -GNinja
-S${PROJECT_SOURCE_DIR}/tests/module_cpp20 -B${PROJECT_BINARY_DIR}/ci_module_cpp20
COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/ci_module_cpp20
)
###############################################################################
# Intel C++ Compiler
###############################################################################

View File

@ -53,7 +53,7 @@ enum class cbor_tag_handler_t
@note from https://stackoverflow.com/a/1001328/266378
*/
static inline bool little_endianness(int num = 1) noexcept
inline bool little_endianness(int num = 1) noexcept
{
return *reinterpret_cast<char*>(&num) == 1;
}

View File

@ -62,7 +62,7 @@ inline StringType escape(StringType s)
* Note the order of escaping "~1" to "/" and "~0" to "~" is important.
*/
template<typename StringType>
static void unescape(StringType& s)
inline void unescape(StringType& s)
{
replace_substring(s, StringType{"~1"}, StringType{"/"});
replace_substring(s, StringType{"~0"}, StringType{"~"});

View File

@ -3126,7 +3126,7 @@ inline StringType escape(StringType s)
* Note the order of escaping "~1" to "/" and "~0" to "~" is important.
*/
template<typename StringType>
static void unescape(StringType& s)
inline void unescape(StringType& s)
{
replace_substring(s, StringType{"~1"}, StringType{"/"});
replace_substring(s, StringType{"~0"}, StringType{"~"});
@ -9868,7 +9868,7 @@ enum class cbor_tag_handler_t
@note from https://stackoverflow.com/a/1001328/266378
*/
static inline bool little_endianness(int num = 1) noexcept
inline bool little_endianness(int num = 1) noexcept
{
return *reinterpret_cast<char*>(&num) == 1;
}

View File

@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.28)
project(json_test CXX)
add_executable(json_test)
target_sources(json_test
PRIVATE main.cpp
PUBLIC FILE_SET cxx_modules TYPE CXX_MODULES FILES json.cpp)
target_compile_features(json_test PUBLIC cxx_std_20)
target_include_directories(json_test PRIVATE ../../include)

View File

@ -0,0 +1,17 @@
module;
#include <nlohmann/json.hpp>
export module json;
export namespace nlohmann
{
using ::nlohmann::adl_serializer;
using ::nlohmann::basic_json;
using ::nlohmann::json_pointer;
using ::nlohmann::json;
using ::nlohmann::ordered_json;
using ::nlohmann::ordered_map;
using ::nlohmann::json_pointer;
} // namespace nlohmann

View File

@ -0,0 +1,6 @@
import json;
int main()
{
nlohmann::json j;
}