Consider the following MCVE, where I have two value arrays where w is two times v (try it out here):
#include <valarray>
using namespace std;
int main() {
valarray<int> v { 1, 2, 3 };
for ([[maybe_unused]] auto x : v) {} // Ok
auto w = v * 2; // Leads to failure in loop below
//valarray<int> w = v * 2; // Works
//auto w = v*=2; // Works
//auto w = v; w *= 2; // Works
for ([[maybe_unused]] auto x : w) {} // Failure here
}
This example fails to compile with clang and gcc at the last loop with (gcc output here):
error: no matching function for call to 'begin(std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_ValArray, std::_Constant, int, int>, int>&)'
The source of the problem seems to be the decuced type of v * 2 (I assume that because explicitly writing down the type works, so some implicit conversion seems to be taking place).
Looking at the reference notes, it seems that operator* may return something different than std::valarray<T>.
I don't understand the reason for this but more puzzling is that the same seem to apply to operator*=, except that here my auto assignment works. I would expect the return values of operator*= and operator* to be the same here (delta the reference).
So my questions are:
- Is this an implementation issue/bug? Or am I missing something?
- What's the rationale behind the reference notes (e.g. why can the operators return something different that may not work with
std::begin/std::end)?
(Note: I tagged this question c++11, but it seems to apply to all versions up to 17 as well)