The whole range for an iterator obtained from a std::span or std::basic_string_view is not clear
Jiang An

Created on 2023-08-29.00:00:00 last changed 1 month ago


Date: 2023-10-15.00:00:00

[ 2023-10-30; Reflector poll ]

Set priority to 3 after reflector poll. Libc++ diagnoses the example with the right macros defined. "Should substr and remove_suffix tighten the bounds or copy them from the original view?"

Date: 2023-08-29.00:00:00

It is unclear whether the following program has undefined behavior:

#include <cassert>
#include <span>
#include <string_view>

int main()
  int arr[2]{42, 84};
  std::span<int> sp1{arr, 1};
  std::span<int> sp2{arr + 1, 1};

  assert(sp2.begin() - sp1.begin() == 1); // Is this well-defined?
  assert(sp2.begin() == sp1.end());       // ditto
  assert(*sp1.end() == 84);               // ditto

  const char str[]{"string"};
  std::string_view sv1{str, 3};
  std::string_view sv2{str + 3, 3};

  assert(sv2.begin() - sv1.begin() == 3); // Is this well-defined?
  assert(sv2.begin() == sv1.end());       // ditto
  assert(*sv1.end() == 'i');              // ditto

Currently MSVC STL strictly diagnose the arithmetic between different spans/basic_string_views, even the views are on the same underlying range (see this Github issue). libstdc++ and libc++ silently accept these operations.

Perhaps the standard should clarify what the whole ranges are when determining whether operations between iterators from different views on the same underlying range is well-defined.

Date User Action Args
2023-10-30 17:22:20adminsetmessages: + msg13796
2023-08-29 00:00:00admincreate