简体   繁体   中英

std:: accumulate division by zero

I do not understand why I get a division by zero in the following code.

template <typename T>
typename std::iterator_traits<T>::value_type avg_abs_difference(T first, T second)
{
    using Type = typename std::iterator_traits<T>::value_type;
    if(first == second)return 0;
    Type oldValue = *first;
    Type count = 0;
    Type res = std::accumulate(std::next(first), second, 0, [&](Type a, Type b)
                           {
                            Type result = a + std::abs(b- oldValue);
                            oldValue = b;
                            ++count;
                            return  result; }
                            )/ count;
    return res ;
}

but when I pull the division outward like this, it does not occur.

template <typename T>
typename std::iterator_traits<T>::value_type avg_abs_difference(T first, T second)
{
    using Type = typename std::iterator_traits<T>::value_type;
    if(first == second)return 0;
    Type oldValue = *first;
    Type count = 0;
    Type res = std::accumulate(std::next(first), second, 0, [&](Type a, Type b)
                           {
                            Type result = a + std::abs(b- oldValue);
                            oldValue = b;
                            ++count;
                            return  result; }
                            );
    return res / count;
}

That's because "count" is evaluated before std::accumulate, so the side effect is not taken into consideration.

https://en.cppreference.com/w/cpp/language/eval_order

Order of evaluation of any part of any expression, including order of evaluation of function arguments is unspecified (with some exceptions listed below). The compiler can evaluate operands and other subexpressions in any order, and may choose another order when the same expression is evaluated again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM