https://rentry.org/PPP2_p96b

// Code derived from Stroustrup's PPP2 book
// § 4.3.1 Constant expressions
// -beginning on p 96

#include <iostream>

using std::cout;

int main()
{
  constexpr int max = 17;  // a literal is a constant expression
  int           val = 19;

  int max2 = max + 2;  // a constant expression (a const int plus a literal)
  int val2 = val + 2;  // not a constant expression: it uses a variable

  cout << max << '\n'   // output above variable values to console...
       << val << '\n'   //
       << max2 << '\n'  //
       << val2 << '\n';
}

build & run:

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

PrevUpNext

Edit
Pub: 21 Jan 2023 15:21 UTC
Edit: 29 Apr 2023 06:42 UTC
Views: 449