https://rentry.org/PPP2_p258

// Code derived from Stroustrup's PPP2 book
// § 8.2 Declarations and definitions
//  -and beginning on p 258

#include <iostream>
#include <vector>

using namespace std;

int f(int);  // declaration of f

int main()
{
  int i = 7;  // declaration of i
  cout << f(i) << '\n';
}

int f(int n) { return n; }  // definition of f

//---

int a = 7;  // definition

vector<double> v;  // declaration

//---

double sqrt(double);                 // declaration, no function body here
double sqrt(double d) { return d; }  // definition, has function body
double sqrt(double);                 // another declaration of sqrt
double sqrt(double);                 // yet another declaration of sqrt

// int sqrt(double);  // error: inconsistent declarations of sqrt

//---

// int a;  // error: double definition of a

extern int a;  // “extern plus no initializer” means “not definition”

extern int x;  // declaration
extern int x;  // another declaration
               //
int x = 7;     // definition

//---

double expression();  // just a declaration; not a definition

double primary()  // definition
{
  // ...

  return expression();
}

double term()  // definition
{
  // ...

  return primary();
}

double expression()  // definition
{
  // ...

  return term();
}

build & run:

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

PrevUpNext

Edit
Pub: 04 Apr 2023 14:03 UTC
Edit: 02 May 2023 22:12 UTC
Views: 581