Skip to content

Specialize char_traits for std::byte to fix from_msgpack (fixes #4756)#4760

Merged
nlohmann merged 5 commits into
nlohmann:developfrom
XueSongTap:develop
Apr 28, 2025
Merged

Specialize char_traits for std::byte to fix from_msgpack (fixes #4756)#4760
nlohmann merged 5 commits into
nlohmann:developfrom
XueSongTap:develop

Conversation

@XueSongTap
Copy link
Copy Markdown
Contributor

Summary

Ensure from_msgpack properly handles containers of std::byte by mapping std::byte to unsigned char.

Changes

  • Specialize char_traits<std::byte> when __cpp_lib_byte >= 201603L.
  • Implement to_int_type, to_char_type, and eof() for std::byte using std::char_traits<char> as a base.
  • Guard specialization with #if defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603L to maintain compatibility with C++11.

Reason

Without char_traits<std::byte>, using from_msgpack on std::vector<std::byte> leads to a appleclang compilation failure, because char_traits<T> is required by the input adapter.

Fixes #4756

Testing Done

  • Verified that existing tests pass (ctest --test-dir build -j 10).
  • Added new test cases in tests/src/unit-msgpack.cpp

Checklist

Additional Note on Test Behavior

The newly added unit test verifying from_msgpack with std::byte containers is only strictly required and triggered on stricter compilers such as:

  • Apple Clang 17.0.0 (clang-1700.0.13.3)
  • Target: arm64-apple-darwin24.2.0

On most GCC and older Clang versions, this specific issue does not manifest because of more relaxed type deduction and char_traits usage, so the corresponding test would pass without triggering a compilation error even without the fix.
However, maintaining the specialization ensures consistent behavior and future-proof compatibility across all supported compilers.

Comment thread tests/src/unit-msgpack.cpp Outdated
Comment thread tests/src/unit-msgpack.cpp Outdated
Comment thread include/nlohmann/detail/meta/type_traits.hpp Outdated
@github-actions
Copy link
Copy Markdown

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @XueSongTap
Please read and follow the Contribution Guidelines.

@coveralls
Copy link
Copy Markdown

coveralls commented Apr 26, 2025

Coverage Status

coverage: 99.189% (+0.001%) from 99.188%
when pulling c178d93 on XueSongTap:develop
into 6b91993 on nlohmann:develop.

@XueSongTap
Copy link
Copy Markdown
Contributor Author

Hi, @nlohmann

This PR adds a missing comment for #include (to clarify that it is needed for std::byte) and improves the English comments in the unit-msgpack.cpp tests for better clarity.

Could you please help take another look to make sure the added comments look good?

Thanks!

@github-actions
Copy link
Copy Markdown

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @XueSongTap
Please read and follow the Contribution Guidelines.

Comment thread include/nlohmann/detail/meta/type_traits.hpp Outdated
@nlohmann nlohmann added the please rebase Please rebase your branch to origin/develop label Apr 27, 2025
@nlohmann
Copy link
Copy Markdown
Owner

…ann#4756)

Provide a char_traits<std::byte> specialization under __cpp_lib_byte
to allow parsing MessagePack data from containers of std::byte.

Signed-off-by: xuesongtap <tap91624@gmail.com>
Signed-off-by: yexiaochuan <tap91624@gmail.com>
Signed-off-by: xuesongtap <tap91624@gmail.com>
Signed-off-by: yexiaochuan <tap91624@gmail.com>
Signed-off-by: yexiaochuan <tap91624@gmail.com>
Signed-off-by: yexiaochuan <tap91624@gmail.com>
@nlohmann nlohmann removed the please rebase Please rebase your branch to origin/develop label Apr 27, 2025
Signed-off-by: yexiaochuan <tap91624@gmail.com>
Copy link
Copy Markdown
Owner

@nlohmann nlohmann left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@nlohmann nlohmann added this to the Release 3.12.1 milestone Apr 28, 2025
@nlohmann nlohmann merged commit 3b02afb into nlohmann:develop Apr 28, 2025
140 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incompatibility of std::char_traits and std::byte in Xcode 16.3

3 participants