https://rentry.org/PPP2_p266

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

#include <iostream>

using namespace std;

void f()
{
  // g();  // error: g() isn't (yet) in scope
}

void g()
{
  f();  // ok: f() is in scope
}

void h()
{
  // int x = y; // error: y isn't (yet) in scope
  int x = 0;
  int y = x;  // ok: x is in scope
  g();        // ok: g() is in scope

  cout << y << '\n';
}

int main() { h(); }

build & run:

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

PrevUpNext

Edit
Pub: 04 Apr 2023 14:27 UTC
Edit: 02 May 2023 23:26 UTC
Views: 311