Update is_string_constructible
for C++23 P2166R1
#1222
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I work on MSVC's C++ Standard Library implementation, and we regularly build sol2 (along with many other open-source projects) to identify compiler/library bugs that would break your project, so we can fix them before shipping. This also allows us to provide advance notice of source-breaking changes - which is the case here.
The paper P2166R1 "Prohibiting
basic_string
Andbasic_string_view
Construction Fromnullptr
" has been accepted for the upcoming C++23 Standard, and we recently implemented it by merging microsoft/STL#1995 from our contributor @sam20908. Our open-source test pass then discovered the following code in sol2 that leads to a compiler error in C++23:sol2/include/sol/traits.hpp
Lines 663 to 666 in bb5f60e
According to my understanding, this type trait lists the various things that
std::basic_string
is constructible from, which allows other conversions in your library to be constrained in order to avoid ambiguity. This has been broken by C++23's addition of astd::basic_string
constructor fromstd::nullptr_t
(which is deleted, but that doesn't affect overload resolution).This PR contains my proposed fix: use
std::is_null_pointer_v
to detect (cv-qualified)std::nullptr_t
. I believe that this can be added unconditionally (i.e. not guarded by Standard mode). First, this type trait was present in C++17 along withstd::is_same_v
being used here. Second, before C++23, attempting to constructstd::basic_string<CharT>
fromstd::nullptr_t
would consider theconst CharT*
constructor (and then lead to UB at runtime), while in C++23 it selects the new constructor. Because a constructor would be considered either way, it seems correct for both types to activate youris_string_constructible
.There may be other ways to fix this C++23 source-breaking change; there may also be additional occurrences of this throughout the sol2 codebase that we haven't encountered yet. Thanks to @cdacamar, MSVC compiler front-end dev, for analyzing and reducing this issue (which I originally mistakenly reported as an MSVC compiler bug).
Here are my instructions for how to reproduce the bug and verify the proposed fix, using our development build of the STL (as the C++23 change to
std::basic_string
is available on GitHub but will take some time to ship in a Visual Studio Preview):Click to expand lengthy repro instructions:
Here is the full compiler output (from VS 2019 16.11 Preview 2 and Clang 11) from the "build" and "verify" steps at the end:
Click to expand lengthy compiler output: