Skip to content

<vector>: Using vector iterators in consteval function results in an error with /D_ITERATOR_DEBUG_LEVEL=2 #5171

Open
@Predelnik

Description

@Predelnik

Describe the bug

Using iterators to iterate std::vector in a loop within consteval function leads to an error "failure was caused by a read of a variable outside its lifetime"

Command-line test case

C:\Temp>type repro.cpp
#include <vector>

consteval void func()
{
    std::vector<int> v;
    for (auto it = v.begin(); it != v.end(); ++it) {}
}

int main()
{
  func();
}

C:\Temp>cl /EHsc /W4 /WX /MDd /std:c++latest .\repro.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.42.34433 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

/std:c++latest is provided as a preview of language features from the latest C++
working draft, and we're eager to hear about bugs and suggestions for improvements.
However, note that these features are provided as-is without support, and subject
to changes or removal as the working draft evolves. See
https://go.microsoft.com/fwlink/?linkid=2045807 for details.

repro.cpp
.\repro.cpp(11): error C7595: 'func': call to immediate function is not a constant expression
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433\include\xmemory(1352): note: failure was caused by a read of a variable outside its lifetime
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433\include\xmemory(1352): note: see usage of 'it'
.\repro.cpp(11): note: the call stack of the evaluation (the oldest call first) is
.\repro.cpp(11): note: while evaluating function 'void func(void)'
.\repro.cpp(5): note: while evaluating function 'std::vector<int,std::allocator<int>>::~vector(void) noexcept'
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433\include\vector(761): note: while evaluating function 'void std::vector<int,std::allocator<int>>::_Tidy(void) noexcept'
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433\include\vector(2045): note: while evaluating function 'void std::_Container_base12::_Orphan_all(void) noexcept'
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433\include\xmemory(1360): note: while evaluating function 'void std::_Container_base12::_Orphan_all_unlocked_v3(void) noexcept'

Expected behavior

No error with /ITERATOR_DEBUG_LEVEL=2

STL version

Microsoft Visual Studio Community 2022 (64-bit) - Current
Version 17.12.0

Additional context

Example on Godbolt
Could be worked around by compiling in release mode or using _ITERATOR_DEBUG_LEVEL=1(or 0)
Hard to tell if the issue is in compiler or in iterator debug level machinery

Metadata

Metadata

Assignees

No one assigned

    Labels

    compilerCompiler work involved

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions