https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120997

            Bug ID: 120997
           Summary: std::span<const bool, Extent>::subspan returns
                    initializer list
           Product: gcc
           Version: 15.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: yuhan at y2research dot com
  Target Milestone: ---

```
#include <span>
#include <iostream>
#include <array>

auto main() -> int {
  std::array<bool, 5> data{};

  std::span<const bool> data_view(data.data(), 5);
  std::cout << "data_view.data():\t" << data_view.data() << '\n';
  std::cout << "data_view.size():\t" << data_view.size() << '\n';
  std::cout << "========================================\n";

  std::span<const bool> data_view_2 = data_view.subspan(0, 5);
  std::cout << "data_view_2.data():\t" << data_view_2.data() << '\n';
  std::cout << "data_view_2.size():\t" << data_view_2.size() << '\n';
  std::cout << "========================================\n";
  return 0;
}
```
(Godbolt: https://godbolt.org/z/1Ta4rrsb5 )
The above code when compiled with "-std=c++26 -Wall -Wextra" prints
```
data_view.data():       0x7ffc7ca19b9b
data_view.size():       5
========================================
data_view_2.data():     0x7ffc7ca19b3e
data_view_2.size():     2
========================================
```

Namely, `data_view_2` is a size 2 span even though it was created with
`data_view.subspan(0, 5)`. It seems like the return object in `subspan` (C++26)
is being falsely interpreted as an initializer list rather than a pointer and
size, causing `data_view_2` to point to the initializer list on stack (which is
immediately invalidated upon return).

This bug does not occur on C++23 (-std=c++23). It also seems to only occur for
`const bool` (not `bool`, not `int`, etc.).

Reply via email to