The code is very dangerous and should be fixed even for GCC and ICC.
You are using static_cast for the type of value, not for reference or pointer. This creates a new temporary valarray object, so a const overload of begin is called (probably not what you intended), and the iterator returned by begin() refers to a temporary one that goes out of scope immediately, so the returned iterator is invalid and dereferences it undefined behavior.
The code will be compiled as follows:
auto begin()->decltype(std::begin(std::declval<std::valarray<T>&>())) { return std::begin(static_cast<std::valarray<T>&>(*this)); /* cast to reference type! ^^^^^^^^^^^^^^^^^ */ }
decltype does not need to allocate this , it just needs to know the type of the call std::begin on valarray<T> , so it does not matter if the type is incomplete, because you do not know t needs a listing.
In the body of the function, the type is considered complete in any case, therefore the cast is valid.
source share