Is there any way to get a warning for std::accumulate when the type of the init
arg doesn't match the contained type you are accumulating over? e.g. in this example we shouldn't accumulate a 32 bit value when iterating over a vector of 64 bit ints. But it's pretty easy to just pass in 0 and forget to pass 0LL. Is there any way to get a warning for this? -Wall -Wextra -Wconversion
doesn't seem to help. I also tried looking for clang tidy checks that may work, but didn't find anything there either.
std::vector<long long> a = {10000000000, 10000000000};
cout << std::accumulate(a.begin(), a.end(), 0) << "\n"; // overflows
cout << std::accumulate(a.begin(), a.end(), 0LL) << "\n"; // prints 20000000000
Oh, I found the right clang-tidy check:
misc-fold-init-type
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With