Title
basic_const_iterator<volatile int*>::operator-> is ill-formed
Status
c++23
Section
[const.iterators]
Submitter
Hewill Kang

Created on 2023-01-06.00:00:00 last changed 13 months ago

Messages

Date: 2023-02-13.10:17:57

Proposed resolution:

This wording is relative to N4917.

  1. Modify [const.iterators] as indicated:

    namespace std {
      template<class I>
        concept not-a-const-iterator = see below;
    
      template<input_iterator Iterator>
      class basic_const_iterator {
        Iterator current_ = Iterator();                             // exposition only
        using reference = iter_const_reference_t<Iterator>;         // exposition only
      
      public:
        […]
        constexpr reference operator*() const;
        constexpr const autovalue_type* operator->() const
           requires is_lvalue_reference_v<iter_reference_t<Iterator>> &&
                    same_as<remove_cvref_t<iter_reference_t<Iterator>>, value_type>;
        […]
      };
    }
    
    […]
    constexpr const autovalue_type* operator->() const
      requires is_lvalue_reference_v<iter_reference_t<Iterator>> &&
               same_as<remove_cvref_t<iter_reference_t<Iterator>>, value_type>;
    

    -7- Returns: If Iterator models contiguous_iterator, to_address(current_); otherwise, addressof(*current_).

Date: 2023-02-13.00:00:00

[ 2023-02-13 Approved at February 2023 meeting in Issaquah. Status changed: Voting → WP. ]

Date: 2023-02-15.00:00:00

[ 2023-02-01; Reflector poll ]

Set status to Tentatively Ready after six votes in favour during reflector poll.

Date: 2023-01-09.18:56:43

Currently, basic_const_iterator::operator-> constrains the value type of the underlying iterator to be only the cv-unqualified type of its reference type, which is true for raw pointers.

However, since it also explicitly specifies returning a pointer to a const value type, this will cause a hard error when the value type is actually volatile-qualified:

std::basic_const_iterator<volatile int*> it;
auto* p = it.operator->(); // invalid conversion from 'volatile int*' to 'const int*'

The proposed resolution changes the return type from const value_type* to const auto*, which makes it deduce the correct type in the above example, i.e. const volatile int*.

History
Date User Action Args
2023-11-22 15:47:43adminsetstatus: wp -> c++23
2023-02-13 10:17:57adminsetmessages: + msg13372
2023-02-13 10:17:57adminsetstatus: voting -> wp
2023-02-06 15:33:48adminsetstatus: ready -> voting
2023-02-01 20:39:10adminsetmessages: + msg13246
2023-02-01 20:39:10adminsetstatus: new -> ready
2023-01-06 16:24:01adminsetmessages: + msg13193
2023-01-06 00:00:00admincreate