https://rentry.org/PPP2_p292

// Code derived from Stroustrup's PPP2 book
// § 8.6.1 Expression evaluation
//  -and beginning on p 292

#include <iostream>
#include <vector>

using namespace std;

vector<int> v(10);  // v is global

void f(int i, int j) { cout << i << ' ' << j << '\n'; }

int main()
{
  int i = 4;

  v[i]   = ++i;                     // don't: undefined order of evaluation
  v[++i] = i;                       // don't: undefined order of evaluation
  int x  = ++i + ++i;               // don't: undefined order of evaluation
  cout << ++i << ' ' << i << '\n';  // don't: undefined order of evaluation
  f(++i, ++i);                      // don't: undefined order of evaluation

  cout << x << '\n';
}

build & run:

g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p292.cpp && ./a.out

PrevUpNext

Edit
Pub: 04 Apr 2023 15:14 UTC
Edit: 03 May 2023 00:43 UTC
Views: 564