Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22075

Iterator increment leading to different results in clang and gcc

$
0
0

I encountered a weird issue in the following code snippet, leading to different results.

Apple clang (tested with 3.8.0 and 11.0) returns the expected value 10 but gcc (tested with 5.4.0 and 9) returns 12.

#include <functional>#include <iostream>#include <vector>int acc(std::function<int(int, int)> func, std::vector<int> operands) {    auto it = operands.begin();    int result = func(*it, *(++it));  // <-- causing issue, ++it not working as expected    if (operands.size() > 2) {        for (++it; it!=operands.end(); ++it) {            result = func(result, *it);        }    }    return result;}int main() {    std::cout << acc([](int a, int b){ return a+b; }, {3, 5, 2}) << std::endl;}

It can be reproduced for example, using rextester.com.

While debugging I found out that the iterator increment ++it seems to be the problem. Replacing it by it+1 followed by a it = it + 1 statement leads to the expected result in both compilers. But why is that handled differently among the compilers?


Viewing all articles
Browse latest Browse all 22075

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>