https://rentry.org/PPP2_p270

// Code derived from Stroustrup's PPP2 book
// § 8.4 Scope
//  -and beginning on p 270

int g(int x)  // g is global; x is local to g
{
  int f = x + 2;  // f is local
  return 2 * f;
}

class C {
 public:
  struct M {
    // ...
  };
  // ...
};

void f()
{
  class L {
    // ...
  };
  // ...
}

void f1()
{
  void g()  // illegal
      {
          // ...
      }...
}

void f2(int x, int y)
{
  if (x > y) {
    // ...
  } else {
    // ...
    {
      // ...
    }
    // ...
  }
}

//------------------------------------------------------------------------------

// clang-format off
// dangerously ugly code:
struct X {
void f(int x) {
struct Y {
int f() { return 1; } int m; };
int m;
m=x; Y m2;
return f(m2.f()); }
int m; void g(int m) {
if (m) f(m+2); else {
g(m+2); }}
X() { } void m3() {
}

void main() {
X a; a.f(2);}
};
// clang-format on

//------------------------------------------------------------------------------

int main()
{
  g(0);
  f();
  f2(0, 1);

  X x;
  x.main();
  x.m3();
}

build & run:

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

PrevUpNext

Edit
Pub: 04 Apr 2023 14:33 UTC
Edit: 02 May 2023 23:30 UTC
Views: 632