Skip to content

<xutility>: basic_const_iterator<int *> cannot convert to basic_const_iterator<const int *> #5321

@ghost

Description

Describe the bug

The conversion selects the following constructor:

template <convertible_to<_Iter> _Other>
constexpr basic_const_iterator(basic_const_iterator<_Other> _Current_)
    noexcept(is_nothrow_constructible_v<_Iter, _Other>) // strengthened
    : _Current(_STD move(_Current_._Current)) {}

However, it fails at _Current_._Current, which is a private member of basic_const_iterator<int *>. This member cannot be accessed by basic_const_iterator<const int *>, as the latter is not a friend of the former.

Command-line test case

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

int main() {
  std::basic_const_iterator<int *> i;
  std::basic_const_iterator<const int *> ci = i;
}

C:\Temp>cl /EHsc /W4 /WX /std:c++latest .\repro.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.43.34808 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
C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.43.34808\include\xutility(2278): error C2248: 'std::basic_const_iterator<int *>::_Current': cannot access private member declared in class 'std::basic_const_iterator<int *>'
C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.43.34808\include\xutility(2244): note: see declaration of 'std::basic_const_iterator<int *>::_Current'
C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.43.34808\include\xutility(2242): note: see declaration of 'std::basic_const_iterator<int *>'
C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.43.34808\include\xutility(2278): note: the template instantiation context (the oldest one first) is
.\repro.cpp(5): note: see reference to function template instantiation 'std::basic_const_iterator<const int *>::basic_const_iterator<int*>(std::basic_const_iterator<int *>) noexcept' being compiled
.\repro.cpp(5): note: see the first reference to 'std::basic_const_iterator<const int *>::basic_const_iterator' in 'main'

Expected behavior

The conversion should be allowed.

STL version

Visual Studio Build Tools 2022
17.13.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions