Here is the code snippet:
#include <iostream>
#include <iterator>
#include <algorithm>
#include <functional>
#include <vector>
std::vector<int> vec(5);
int produce_seq()
{
static int value = 0;
return (value*value++);
}
int main()
{
std::generate_n(vec.begin(),5, produce_seq);
for(auto val:vec)
{
std::cout << val <<std::endl;
}
}
Why this code snippet ouputs
0
2
6
12
20
other than
0
1
4
9
16
I think value*value++;
is equivalent to value*value; value++;
.
UPDATED:
Should not value++
be evaluated after the entire expression has already been evaluated? For example, if there is a expression like sum=a+b++;
, sum=a+b;
is always evaluated before b++
.