fix: define _BACKUP_ITERATOR_DEBUG_LEVEL for MSVC 17.12+ compatibility#5957
fix: define _BACKUP_ITERATOR_DEBUG_LEVEL for MSVC 17.12+ compatibility#5957loumalouomega wants to merge 2 commits intopybind:masterfrom
Conversation
…BACKUP_ITERATOR_DEBUG_LEVEL' undeclared when including span header from STL.
Original Prompt (Cursor Claude Opus 4.5)
AnalysisPR SummaryPR #5957 proposes adding this to // See Issue #5956. This fixes compilation failure with MSVC 17.12 (v14.44) and C++20:
// '_BACKUP_ITERATOR_DEBUG_LEVEL' undeclared when including span header from STL.
#if defined(_MSC_VER)
# ifndef _BACKUP_ITERATOR_DEBUG_LEVEL
# define _BACKUP_ITERATOR_DEBUG_LEVEL 0
# endif
#endifThe issue (#5956) reports a compilation failure when building Kratos Multiphysics with MSVC 17.12 and C++20, where the Key Finding: The pybind11 CI Already Tests This ScenarioLooking at the CI configuration in - runs-on: windows-latest
python-version: '3.14'
cmake-args: -DCMAKE_CXX_STANDARD=20The GitHub Actions Additional Windows C++20+ tests that already exist:
Why This Issue Doesn't Reproduce in pybind11 CI
The Real ProblemThe The fact that this macro is "undeclared" suggests Kratos is likely doing something that interferes with the STL's internal iterator debugging mechanism—possibly:
Why the Proposed Fix is ProblematicBlindly defining
RecommendationThis fix should NOT be merged into pybind11. Reasons:
The Fix Belongs in KratosKratos should either:
Alternative Client-Side WorkaroundIf Kratos users need an immediate workaround, they can add this to their own code before including pybind11: // Workaround for MSVC 17.12 iterator debug macro issue
// Add this BEFORE including any pybind11 or STL headers
#if defined(_MSC_VER)
# include <yvals_core.h>
#endif
#include <pybind11/pybind11.h>No CI Changes NeededSince:
There's no CI change that would help reproduce this issue, because the issue is not in pybind11. Summary
|
Description
This PR fixes a compilation error encountered when using Visual Studio 2022 version 17.12 (MSVC 14.44) in C++20 mode.
The MSVC STL (specifically the
<spanheader) has introduced internal checks that reference the macro_BACKUP_ITERATOR_DEBUG_LEVEL. In certain build environments—particularly those involving complex Python bindings like Kratos Multiphysics—this macro is expected by the STL but remains undeclared, leading to the following error:Proposed Changes
Inserted a preprocessor guard in
include/pybind11/pybind11.hto ensure_BACKUP_ITERATOR_DEBUG_LEVELis defined to0if it is not already set. This allows the MSVC STL headers to resolve their internal iterator debugging logic correctly. I understand that maybe the placement can be improved in order to not add "garbage" to the main include file, I am open to suggestions.Motivation and Context
<spanthrough or alongsidepybind11.How Has This Been Tested?
Suggested changelog entry: