Aligned allocation fix for clang-cl #1988
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.
This is a tentative fix for an issue I have encountered while trying to build a pybind11 extension using clang-cl on Windows.
As a bit of background, clang-cl is essentially a drop-in binary-compatible replacement for the
cl.exe
compiler from MSVC, based on LLVM/clang. This means that one can install clang-cl on top of an existing MSVC installation, and then clang-cl can be used as a compiler in conjunction with the standard C++ library provided with MSVC.The issue I ran into is that, on my specific setup, the type
std::align_val_t
is not available, but pybind11 is trying anyway to employ it. This happens because of the following:std::align_val_t
, but, at the same time,__cpp_aligned_new
,std::align_val_t
(and aligned allocation more generally) is available,hence the compilation error.
This is admittedly a bit of a corner-case setup, but, as far as I know, it is the only setup currently available on conda/conda-forge which allows to compile and package moderately-complicated C++17 software on Windows. conda-forge uses by default an older version of MSVC, which is not practically usable for C++17 development. Using the clang-cl compiler allows to sidestep the deficiencies of the MSVC compiler while at the same time retaining binary compatibility with it.
The proposed fix essentially runs a version check on the
_MSC_VER
variable, rather than trusting the presence of__cpp_aligned_new
. According to this webpage,https://docs.microsoft.com/en-us/cpp/build/reference/zc-alignednew?view=vs-2019
aligned allocation is available on MSVC since Visual Studio 2017 version 15.5 (
_MSC_VER
1912).