<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/76393>76393</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[C++20] Problem parsing pipe syntax as valarray's operator|
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
yihuajack
</td>
</tr>
</table>
<pre>
Reproducer:
```c++
#include <iostream>
#include <ranges>
#include <valarray>
int main() {
std::valarray<double> va1 = {1, 2};
std::valarray<double> va2 = {3, 4};
for (const auto& elem : va1 * va2 | std::views::transform([](double x) {return x + 1;})) {
std::cout << "- " << elem << '\n';
}
}
```
This code can be successfully compiled by x64 Microsoft C/C++ compiler 19.38.33130 by `cl /EHsc /std:c++20 test.cpp`, but failed to be compiled by `clang++ -std=c++20 -stdlib=libc++ test.cpp`.
If writing `std::valarray<double> vprod = va1 * va2;` and
```c++
for (const auto& elem : vprod | std::views::transform([](double x) {return x + 1;}))
```
The code can also be successfully compiled by clang with libc++.
clang version 17.0.6 to trunk--20231226 produce this.
>[<source>:10:70: error: invalid operands to binary expression ('__val_expr<_BinaryOp<multiplies<typename valarray<double>::value_type>, valarray<double>, valarray<double>>>' (aka '__val_expr<_BinaryOp<multiplies<double>, std::valarray<double>, std::valarray<double>>>') and '__range_adaptor_closure_t<__bind_back_t<__fn, tuple<(lambda at <source>:10:94)>>>' (aka 'std::__range_adaptor_closure_t<std::__bind_back_t<std::ranges::views::__transform::__fn, std::tuple<(lambda at <source>:10:94)>>>'))](error: invalid operands to binary expression ('__val_expr<_BinaryOp<multiplies<typename valarray<double>::value_type>, valarray<double>, valarray<double>>>' (aka '__val_expr<_BinaryOp<multiplies<double>, std::valarray<double>, std::valarray<double>>>') and '__range_adaptor_closure_t<__bind_back_t<__fn, tuple<(lambda at <source>:8:67)>>>' (aka 'std::__range_adaptor_closure_t<std::__bind_back_t<std::ranges::views::__transform::__fn, std::tuple<(lambda at <source>:8:67)>>>')))
According to [operator+,-,*,/,%,&,|,^,<<,>>,&&,|| (std::valarray)](https://en.cppreference.com/w/cpp/numeric/valarray/operator_arith3):
```c++
template< class T >
std::valarray<T> operator* ( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator| ( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
```
It seems that it is reasonable to parse the "|" as pipe syntax rather than valarray's operator|., evaluating `va1 * va2` as a `std::valarray`.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsV0tv4zYQ_jX0ZWBDJm3JPvjgyGt0D0WLYu8GRY1tbihSIKkk_vfFUPIjWScpiu5hgQoCJZHz-ObB4UiGoA8WccXmD2y-GckuHp1fnfSxk9-lehxVrj6t_sLWu7pT6JlYs2zDsjXLs_5WjD_Q3c9yoa0yXY3ARKldiB5lw8SXe8te2gOGdxafpJHey9N1OY3aRmiktowvGF8CKwbFMFwh1gRRrK_8Ze26yiATX-BJToGJDbFNGS-Bs2LDxFnEZ7z8zCuId_aKd7j2zgPjC-VsiCC76BjPAQ02wMS6V8_XvaiivFGo8Tn0r9FLG_bON2Rhignjix4FvAwme4ydt_ACjD_AlEAUG8aXrx1yEa5cF8mjTJTAOB_TcP4eoA1rBZuXlh5vzSL5fRAuL-fw95_fjjqAcjWCkhYqhNAphSHsO2NOoFzTaoM1VCd4yWfwu1beBbePUDK-LfsEOlN5mC4nYjERYioy4qAcM8D49stvQdGzt2zIO55BxBAnqm0JDS-h6iLsZdIXHWG5VZ-ESXsYdI6TrM1VFk0YXTGxMboapm8VTHp7v-7h2euo7YEkfpI4tHdS6tzEn1ycZyBt_dF2-iSdesE_JZHeiTJegyxNcB9GOvkZnnU8wtWXg__6tSf0QTsL02KSTXKKVvSdfRyPecbFlPMchrID8ajDwEoFYf7ARBlc5xV5mIn1NGNiXdAA6L2jMgXaPkmja3AtemnrkLJBW-lPgC-tx5B0p0JS7HZP0uxomo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