COMP: Pass /Zc:__cplusplus when compiling SWIG wrappers with MSVC - #6822
Conversation
a19985b to
8b09530
Compare
|
dzenanz
left a comment
There was a problem hiding this comment.
Greptile is right. This comment does not need to be that long. The full explanation is in the commit message. A suggested solution is provided inline. Otherwise seems like a good fix.
MSVC reports __cplusplus as 199711L regardless of /std:c++NN unless
/Zc:__cplusplus is given. SWIG guards its move support on
__cplusplus >= 201103L, so SWIG_STD_MOVE(x) silently degraded from
std::move(x) to x, and every wrapped method taking a std::unique_ptr
by value failed to compile.
GradientImageFilter::SetBoundaryCondition is the first such method, so
the Windows Python wrapping build broke with
itkGradientImageFilterPython.cpp: error C2280:
'std::unique_ptr<itk::ImageBoundaryCondition<...>>::unique_ptr(
const unique_ptr &)': attempting to reference a deleted function
GCC and Clang report __cplusplus truthfully and emit std::move, which
is why only the MSVC Python wrapping build was affected.
The flag joins the existing MSVC wrapper flags, so it is scoped to the
generated wrapper sources and does not change how ITK itself is built.
It also covers any other SWIG feature gated on __cplusplus.
8b09530 to
9ceb493
Compare
445b19d
into
InsightSoftwareConsortium:main
|
@greptileai review |
Problem
The Windows Python wrapping build fails with five
C2280errors, all from one root cause:Root cause
Not the wrapping declarations. SWIG generates correct code:
The
%unique_ptr(...)typemap applies as intended and the one-argument mangled name resolves againstImageBoundaryCondition<TInputImage, TInputImage>without trouble, since the default template argumentcollapses for SWIG just as it does for C++.
The failure is in SWIG's move guard:
MSVC reports
__cplusplusas199711Lregardless of/std:c++NNunless/Zc:__cplusplusis given.ITK passes
/std:c++17but never/Zc:__cplusplus. On MSVC the#elsebranch therefore wins,SWIG_STD_MOVE(arg2)degrades fromstd::move(arg2)to plainarg2, and the by-valuestd::unique_ptrparameter is copied.
Measured on the CI toolchain (MSVC 19.38.33145):
__cplusplus/std:c++17199711L/std:c++17 /Zc:__cplusplus201703Lstd::moveGCC and Clang report
__cplusplustruthfully and emitstd::move, which is why only the MSVC Pythonwrapping build is affected. It is not that other platforms wrap a different instantiation set.
GradientImageFilter::SetBoundaryCondition(added in 6203371) is simply the first wrapped method to takea
std::unique_ptrby value, so it is the first to trip a pre-existing gap in the MSVC wrapper flags ratherthan the cause of it.
Fix
Add
/Zc:__cplusplusto the MSVC wrapper compile flags, alongside the/wd4244 /wd4996already there. Thisis scoped to the SWIG-generated wrapper sources, so it does not change how ITK itself is compiled, and it
covers any other SWIG feature gated on
__cplusplusrather than only this call site.No
.wrapfile or header change is required; the generated.cppis unchanged by this fix.Verification
Reproduced and fixed on Windows, MSVC 19.38.33145 (VS 2022, toolset 14.38), Ninja, Release x64, against
mainat 2bdb783.Before:
After:
ITKImageGradientPythonbuilds and links clean — 0 errors, 0C2280, producing_ITKImageGradientPython.pyd. SWIG did not regenerate the.cpp; only the compile flags changed, confirmingthis is purely a flag defect.
Scope of verification: the reproduction used a reduced wrapping configuration
(
Module_ITKImageGradientonly,ITK_WRAP_float=ON,ITK_WRAP_IMAGE_DIMS=2;3) to keep the turnaround short.The flag applies to every wrapper target, so a full
ITK_WRAP_PYTHONbuild is the appropriate regressioncheck before merging; that has not been run here.
AI assistance
C2280past the wrapping declarations toSWIG's
SWIG_STD_MOVEguard, and measured__cpluspluswith andwithout
/Zc:__cpluspluson the CI toolchain to confirm