<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/60143>60143</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [libc++] Incorrect type for __init in transform_inclusive_scan when not passed explicitly?
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          q-p
      </td>
    </tr>
</table>

<pre>
    When using `transform_inclusive_scan` (and probably also applying to the `exclusive` variant) without passing `init` the type used for the internally created `__init` seems either wrong or too specific.

Consider the following example
```cpp
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
 
int main()
{
 std::vector<std::vector<int> > rows{std::vector<int>{1}, std::vector<int>{}, std::vector<int>{1, 2}};

  // want 0, 1, 1, 3
 std::vector<std::size_t> offsets(rows.size() + 1);

 std::transform_inclusive_scan(rows.cbegin(), rows.cend(), offsets.begin() + 1, std::plus{},
    [](const std::vector<int> &row)
    {
 return row.size();
    }
#if 0
    , std::size_t{0}
#endif
 );

  for (const auto s : offsets)
    std::cout << s << ' ';
 std::cout << std::endl;
}
```

In particular
```
/opt/homebrew/opt/llvm/bin/../include/c++/v1/__numeric/transform_inclusive_scan.h:45:62: error: no viable conversion from 'std::vector<int>::size_type' (aka 'unsigned long') to 'typename iterator_traits<__wrap_iter<const vector<int> *>>::value_type' (aka 'std::vector<int>')
        typename iterator_traits<_InputIterator>::value_type __init = __u(*__first);
 ^ ~~~~~~~~~~~~~
```
seems wrong since applying the unary function will in general *NOT* return the type of the input iterator (since that's the point of the `transform`).


When not passing `init`, the following errors are displayed

```
In file included from /opt/homebrew/opt/llvm/bin/../include/c++/v1/numeric:163:
/opt/homebrew/opt/llvm/bin/../include/c++/v1/__numeric/transform_inclusive_scan.h:45:62: error: no viable conversion from 'std::vector<int>::size_type' (aka 'unsigned long') to 'typename iterator_traits<__wrap_iter<const vector<int> *>>::value_type' (aka 'std::vector<int>')
        typename iterator_traits<_InputIterator>::value_type __init = __u(*__first);
 ^ ~~~~~~~~~~~~~
test.cpp:14:8: note: in instantiation of function template specialization 'std::transform_inclusive_scan<std::__wrap_iter<const std::vector<int> *>, std::__wrap_iter<unsigned long *>, std::plus<>, (lambda at test.cpp:15:5)>' requested here
 std::transform_inclusive_scan(rows.cbegin(), rows.cend(), offsets.begin() + 1, std::plus{},
 ^
/opt/homebrew/opt/llvm/bin/../include/c++/v1/vector:451:35: note: candidate constructor not viable: no known conversion from 'std::vector<int>::size_type' (aka 'unsigned long') to 'const std::vector<int> &' for 1st argument
    _LIBCPP_CONSTEXPR_AFTER_CXX17 vector(const vector& __x);
 ^
/opt/homebrew/opt/llvm/bin/../include/c++/v1/vector:458:5: note: candidate constructor not viable: no known conversion from 'std::vector<int>::size_type' (aka 'unsigned long') to 'initializer_list<std::vector<int, std::allocator<int>>::value_type>' (aka 'initializer_list<int>') for 1st argument
 vector(initializer_list<value_type> __il);
 ^
/opt/homebrew/opt/llvm/bin/../include/c++/v1/vector:469:5: note: candidate constructor not viable: no known conversion from 'std::vector<int>::size_type' (aka 'unsigned long') to 'std::vector<int> &&' for 1st argument
    vector(vector&& __x)
 ^
/opt/homebrew/opt/llvm/bin/../include/c++/v1/vector:369:70: note: explicit constructor is not a candidate
    _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a)
 ^
/opt/homebrew/opt/llvm/bin/../include/c++/v1/vector:379:44: note: explicit constructor is not a candidate
    _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit vector(size_type __n);
 ^
In file included from test.cpp:4:
In file included from /opt/homebrew/opt/llvm/bin/../include/c++/v1/numeric:163:
/opt/homebrew/opt/llvm/bin/../include/c++/v1/__numeric/transform_inclusive_scan.h:46:23: error: assigning to 'unsigned long' from incompatible type 'typename iterator_traits<__wrap_iter<const vector<int> *>>::value_type' (aka 'std::vector<int>')
        *__result++ = __init;
 ^~~~~~
/opt/homebrew/opt/llvm/bin/../include/c++/v1/__numeric/transform_inclusive_scan.h:31:18: error: no matching function for call to object of type 'std::plus<>'
 __init = __b(__init, __u(*__first));
 ^~~
/opt/homebrew/opt/llvm/bin/../include/c++/v1/__numeric/transform_inclusive_scan.h:48:27: note: in instantiation of function template specialization 'std::transform_inclusive_scan<std::__wrap_iter<const std::vector<int> *>, std::__wrap_iter<unsigned long *>, std::vector<int>, std::plus<>, (lambda at test.cpp:15:5)>' requested here
            return _VSTD::transform_inclusive_scan(__first, __last, __result, __b, __u, __init);
                          ^
test.cpp:14:8: note: in instantiation of function template specialization 'std::transform_inclusive_scan<std::__wrap_iter<const std::vector<int> *>, std::__wrap_iter<unsigned long *>, std::plus<>, (lambda at test.cpp:15:5)>' requested here
  std::transform_inclusive_scan(rows.cbegin(), rows.cend(), offsets.begin() + 1, std::plus{},
 ^
/opt/homebrew/opt/llvm/bin/../include/c++/v1/__functional/operations.h:46:10: note: candidate template ignored: substitution failure [with _T1 = std::vector<int> &, _T2 = unsigned long]: invalid operands to binary expression ('std::vector<int>' and 'unsigned long')
    auto operator()(_T1&& __t, _T2&& __u) const
         ^
In file included from test.cpp:4:
In file included from /opt/homebrew/opt/llvm/bin/../include/c++/v1/numeric:163:
/opt/homebrew/opt/llvm/bin/../include/c++/v1/__numeric/transform_inclusive_scan.h:32:21: error: assigning to 'unsigned long' from incompatible type 'std::vector<int>'
        *__result = __init;
 ^~~~~~
4 errors generated.
```

When passing init (i.e. changing the `#ifdef`) it seems to work as expected.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWUtv4zgS_jX0hRjDIuXXwQc_YsDAoKcxHcz2ngRaKtncpkkNScXJHPa3L4p6WE7kZIDt9DZ62-g4aalKLH5VH-sjJZyTBw2wIOMVGW8GovRHYxd__lIM9iZ7WvzjCJqWTuoDJZORt0K73NhTInWqSicfIHGp0GQyooTNhM5oYc1e7NUTFcoZK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