177
some stuff about C++ and development

Some stuff about C++ and development

Embed Size (px)

DESCRIPTION

A talk I first gave at Amadeus in Nice, 18th April 2013. Includes the #include test.

Citation preview

Page 1: Some stuff about C++ and development

some stuff about C++ and development

Page 2: Some stuff about C++ and development

sequence points

5.1.2.3 Program execution. Clause 2

At certain specified points in the execution sequence called

sequence points, all side effects of previous evaluations shall be

complete and no side effects of subsequent evaluation shall have

taken place.

Page 11: Some stuff about C++ and development

sequence points

Most C and C++ programmers use an implicit mental model made up of lots of sequence points,

progressing in a mostly left-to-right order.

i + v[++i] + v[++i]

Page 12: Some stuff about C++ and development

sequence points

Most C and C++ programmers use an implicit mental model made up of lots of sequence points,

progressing in a mostly left-to-right order.

i + v[++i] + v[++i]

Page 13: Some stuff about C++ and development

sequence points

Most C and C++ programmers use an implicit mental model made up of lots of sequence points,

progressing in a mostly left-to-right order.

i + v[++i] + v[++i]

Page 14: Some stuff about C++ and development

sequence points

Most C and C++ programmers use an implicit mental model made up of lots of sequence points,

progressing in a mostly left-to-right order.

i + v[++i] + v[++i]

Page 15: Some stuff about C++ and development

sequence points

Most C and C++ programmers use an implicit mental model made up of lots of sequence points,

progressing in a mostly left-to-right order.

i + v[++i] + v[++i]

Page 16: Some stuff about C++ and development

sequence points

Most C and C++ programmers use an implicit mental model made up of lots of sequence points,

progressing in a mostly left-to-right order.

i + v[++i] + v[++i]

Page 17: Some stuff about C++ and development

sequence points

Most C and C++ programmers use an implicit mental model made up of lots of sequence points,

progressing in a mostly left-to-right order.

i + v[++i] + v[++i]

Page 18: Some stuff about C++ and development

sequence points

Most C and C++ programmers use an implicit mental model made up of lots of sequence points,

progressing in a mostly left-to-right order.

i + v[++i] + v[++i]

Page 19: Some stuff about C++ and development

sequence points

Most C and C++ programmers use an implicit mental model made up of lots of sequence points,

progressing in a mostly left-to-right order.

i + v[++i] + v[++i]

Page 20: Some stuff about C++ and development

sequence points

Most C and C++ programmers use an implicit mental model made up of lots of sequence points,

progressing in a mostly left-to-right order.

i + v[++i] + v[++i]

Page 21: Some stuff about C++ and development

sequence points

In reality C and C++ have very few sequence points(this is to give maximum scope for optimization).

Page 22: Some stuff about C++ and development

sequence points where?

• at the end of a full expression

• after the first operand of these operators && || ?: ,

• after evaluation of all arguments and the function call expression in a function call

Page 23: Some stuff about C++ and development

only sequence points

govern the order of evaluation

Page 24: Some stuff about C++ and development

got it?

Page 25: Some stuff about C++ and development

sequence points

i = v[++i] + v[++i];

question: where are the sequence points in this statement ?

Page 26: Some stuff about C++ and development

sequence points

i = v[++i] + v[++i];

answer: here!

Page 27: Some stuff about C++ and development

why does this matter?

Page 28: Some stuff about C++ and development

I'm glad you asked!

Page 29: Some stuff about C++ and development

here's why...

Page 30: Some stuff about C++ and development

shall

If a "shall" or a "shall not" requirement that appears outside of a constraint is

violated, the behavior is undefined.

4. Conformance. Clause 2

undefined behavior: behavior ... for which this International Standard imposes no requirements.

3. Terms, definitions, and symbols

Page 31: Some stuff about C++ and development

sequence point: rule-one

Between the previous and next sequence point an object shall have its stored value modified at most once by

the evaluation of an expression.

n = n++;

6.5 Expressions. Clause 2.

undefined behavior

Page 32: Some stuff about C++ and development

sequence point: rule-one

Between the previous and next sequence point an object shall have its stored value modified at most once by

the evaluation of an expression.

n = n++;

6.5 Expressions. Clause 2.

undefined behavior

Page 33: Some stuff about C++ and development

sequence point: rule-two

Between the previous and next sequence point the prior value shall be read only to determine the value to be

stored.

n + n++;

6.5 Expressions. Clause 2.

undefined behavior

Page 34: Some stuff about C++ and development

sequence point: rule-two

Between the previous and next sequence point the prior value shall be read only to determine the value to be

stored.

n + n++;

6.5 Expressions. Clause 2.

undefined behavior

Page 35: Some stuff about C++ and development

example#include <stdio.h>

int main(void){ int v[] = { 0,2,4,6,8 }; int i = 1; int n = i + v[++i] + v[++i]; printf("%d\n", n);}

$ gcc foo.c && ./a.out

gcc

(Mac OS 10.8.2)

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 36: Some stuff about C++ and development

example#include <stdio.h>

int main(void){ int v[] = { 0,2,4,6,8 }; int i = 1; int n = i + v[++i] + v[++i]; printf("%d\n", n);}

$ gcc foo.c && ./a.out

gcc

(Mac OS 10.8.2)

$ gcc foo.c && ./a.out12

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 37: Some stuff about C++ and development

example#include <stdio.h>

int main(void){ int v[] = { 0,2,4,6,8 }; int i = 1; int n = i + v[++i] + v[++i]; printf("%d\n", n);}

$ gcc foo.c && ./a.out

gcc

$ clang foo.c && ./a.out

clang

(Mac OS 10.8.2)

$ gcc foo.c && ./a.out12

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 38: Some stuff about C++ and development

example#include <stdio.h>

int main(void){ int v[] = { 0,2,4,6,8 }; int i = 1; int n = i + v[++i] + v[++i]; printf("%d\n", n);}

$ gcc foo.c && ./a.out

gcc

$ clang foo.c && ./a.out

clang

(Mac OS 10.8.2)

$ gcc foo.c && ./a.out12

$ clang foo.c && ./a.out11

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 39: Some stuff about C++ and development

example#include <stdio.h>

int main(void){ int v[] = { 0,2,4,6,8 }; int i = 1; int n = i + v[++i] + v[++i]; printf("%d\n", n);}

$ gcc foo.c && ./a.out

gcc

$ clang foo.c && ./a.out

clang

$ icc foo.c && ./a.out

icc

(Mac OS 10.8.2)

$ gcc foo.c && ./a.out12

$ clang foo.c && ./a.out11

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 40: Some stuff about C++ and development

example#include <stdio.h>

int main(void){ int v[] = { 0,2,4,6,8 }; int i = 1; int n = i + v[++i] + v[++i]; printf("%d\n", n);}

$ gcc foo.c && ./a.out

gcc

$ clang foo.c && ./a.out

clang

$ icc foo.c && ./a.out

icc

(Mac OS 10.8.2)

$ gcc foo.c && ./a.out12

$ clang foo.c && ./a.out11

$ icc foo.c && ./a.out13

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 42: Some stuff about C++ and development

example

#include <stdio.h>

int main(void) {    int src[] = { 1,2,3,4 };    int dst[] = { 0,0,0,0 };    int i = 1;    dst[i] = src[i++];    printf("%d %d %d %d\n", dst[0], dst[1], dst[2], dst[3]);}

$ gcc *.c && ./a.out

gcc 4.8.0 20130210

Page 43: Some stuff about C++ and development

example

#include <stdio.h>

int main(void) {    int src[] = { 1,2,3,4 };    int dst[] = { 0,0,0,0 };    int i = 1;    dst[i] = src[i++];    printf("%d %d %d %d\n", dst[0], dst[1], dst[2], dst[3]);}

$ gcc *.c && ./a.out$ gcc *.c && ./a.out0 2 0 0

gcc 4.8.0 20130210

Page 44: Some stuff about C++ and development

example

#include <stdio.h>

int main(void) {    int src[] = { 1,2,3,4 };    int dst[] = { 0,0,0,0 };    int i = 1;    dst[i] = src[i++];    printf("%d %d %d %d\n", dst[0], dst[1], dst[2], dst[3]);}

$ gcc *.c && ./a.out$ gcc *.c && ./a.out0 2 0 0

gcc 4.8.0 20130210

#include <stdio.h>

int main(void) {    int src[] = { 1,2,3,4 };    int dst[] = { 0,0,0,0 };    int i = 1;    *(dst + i) = *(src + i++);    printf("%d %d %d %d\n", dst[0], dst[1], dst[2], dst[3]);}

Page 45: Some stuff about C++ and development

example

#include <stdio.h>

int main(void) {    int src[] = { 1,2,3,4 };    int dst[] = { 0,0,0,0 };    int i = 1;    dst[i] = src[i++];    printf("%d %d %d %d\n", dst[0], dst[1], dst[2], dst[3]);}

$ gcc *.c && ./a.out $ gcc *.c && ./a.out$ gcc *.c && ./a.out0 2 0 0

gcc 4.8.0 20130210

#include <stdio.h>

int main(void) {    int src[] = { 1,2,3,4 };    int dst[] = { 0,0,0,0 };    int i = 1;    *(dst + i) = *(src + i++);    printf("%d %d %d %d\n", dst[0], dst[1], dst[2], dst[3]);}

Page 46: Some stuff about C++ and development

example

#include <stdio.h>

int main(void) {    int src[] = { 1,2,3,4 };    int dst[] = { 0,0,0,0 };    int i = 1;    dst[i] = src[i++];    printf("%d %d %d %d\n", dst[0], dst[1], dst[2], dst[3]);}

$ gcc *.c && ./a.out $ gcc *.c && ./a.out$ gcc *.c && ./a.out0 2 0 0

$ gcc *.c && ./a.out0 0 2 0

gcc 4.8.0 20130210

#include <stdio.h>

int main(void) {    int src[] = { 1,2,3,4 };    int dst[] = { 0,0,0,0 };    int i = 1;    *(dst + i) = *(src + i++);    printf("%d %d %d %d\n", dst[0], dst[1], dst[2], dst[3]);}

Page 47: Some stuff about C++ and development
Page 48: Some stuff about C++ and development

precedence is not the same as order of

evaluation

Page 49: Some stuff about C++ and development

order of evaluation is mostly unspecified

and is a run-time thing

Page 50: Some stuff about C++ and development

precedence iscompletely specified

and is a compile-time thing

Page 51: Some stuff about C++ and development

precedence determines which operands

bind to which operators

Page 52: Some stuff about C++ and development

a * b + c

example

Page 53: Some stuff about C++ and development

a * b + c?

Page 54: Some stuff about C++ and development

a * b + c?

Page 55: Some stuff about C++ and development

a() * b() + c()

Page 56: Some stuff about C++ and development

temp_a = a();temp_b = b();temp_c = c();a * b + c

Page 57: Some stuff about C++ and development

temp_c = c();temp_b = b();temp_a = a();a * b + c

Page 58: Some stuff about C++ and development

temp_b = b();temp_c = c();temp_a = a();a * b + c

Page 59: Some stuff about C++ and development

example#include <iostream>...int main(){ int sum = a() + b(); std::cout << sum;}

#include <iostream>...int a(){ std::cout << "a"; return 3;}

#include <iostream>...int b(){ std::cout << "b"; return 4;}

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 60: Some stuff about C++ and development

example#include <iostream>...int main(){ int sum = a() + b(); std::cout << sum;}

$ g++ *.c && ./a.out

#include <iostream>...int a(){ std::cout << "a"; return 3;}

#include <iostream>...int b(){ std::cout << "b"; return 4;}

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 61: Some stuff about C++ and development

example#include <iostream>...int main(){ int sum = a() + b(); std::cout << sum;}

$ g++ *.c && ./a.out

#include <iostream>...int a(){ std::cout << "a"; return 3;}

#include <iostream>...int b(){ std::cout << "b"; return 4;}

$ g++ *.c && ./a.outab7

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 62: Some stuff about C++ and development

example#include <iostream>...int main(){ int sum = a() + b(); std::cout << sum;}

$ g++ *.c && ./a.out $ g++ *.c && ./a.out

#include <iostream>...int a(){ std::cout << "a"; return 3;}

#include <iostream>...int b(){ std::cout << "b"; return 4;}

$ g++ *.c && ./a.outab7

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 63: Some stuff about C++ and development

example#include <iostream>...int main(){ int sum = a() + b(); std::cout << sum;}

$ g++ *.c && ./a.out $ g++ *.c && ./a.out

#include <iostream>...int a(){ std::cout << "a"; return 3;}

#include <iostream>...int b(){ std::cout << "b"; return 4;}

$ g++ *.c && ./a.outab7

$ g++ *.c && ./a.outba7

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 64: Some stuff about C++ and development

indeterminate value

6.2.4 Storage duration of objects

The initial value of the object is indeterminate.

J.2 Undefined behavior

The value of an object with automatic storage duration is used while it is indeterminate.

Page 65: Some stuff about C++ and development

example#include <stdio.h>#include <stdbool.h>

void bar(void){ bool b; if (b) printf("true\n"); if (!b) printf("false\n");}

void foo(void);void bar(void);

int main(void){ foo(); bar();}

void foo(void){ char c = 2; ...}

gcc

Without optimization...

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 66: Some stuff about C++ and development

example#include <stdio.h>#include <stdbool.h>

void bar(void){ bool b; if (b) printf("true\n"); if (!b) printf("false\n");}

void foo(void);void bar(void);

int main(void){ foo(); bar();}

void foo(void){ char c = 2; ...}

gcc

Without optimization...

true

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 67: Some stuff about C++ and development

example#include <stdio.h>#include <stdbool.h>

void bar(void){ bool b; if (b) printf("true\n"); if (!b) printf("false\n");}

void foo(void);void bar(void);

int main(void){ foo(); bar();}

void foo(void){ char c = 2; ...}

gcc clang

Without optimization...

true

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 68: Some stuff about C++ and development

example#include <stdio.h>#include <stdbool.h>

void bar(void){ bool b; if (b) printf("true\n"); if (!b) printf("false\n");}

void foo(void);void bar(void);

int main(void){ foo(); bar();}

void foo(void){ char c = 2; ...}

gcc clang

Without optimization...

true false

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 69: Some stuff about C++ and development

example#include <stdio.h>#include <stdbool.h>

void bar(void){ bool b; if (b) printf("true\n"); if (!b) printf("false\n");}

void foo(void);void bar(void);

int main(void){ foo(); bar();}

void foo(void){ char c = 2; ...}

gcc clang icc

Without optimization...

true false

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 70: Some stuff about C++ and development

example#include <stdio.h>#include <stdbool.h>

void bar(void){ bool b; if (b) printf("true\n"); if (!b) printf("false\n");}

void foo(void);void bar(void);

int main(void){ foo(); bar();}

void foo(void){ char c = 2; ...}

gcc clang icc

Without optimization...

true false truefalse

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 71: Some stuff about C++ and development

example#include <stdio.h>#include <stdbool.h>

void bar(void){ bool b; if (b) printf("true\n"); if (!b) printf("false\n");}

void foo(void);void bar(void);

int main(void){ foo(); bar();}

void foo(void){ char c = 2; ...}

gcc clang icc

Without optimization...

With optimization...

true false truefalse

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 72: Some stuff about C++ and development

example#include <stdio.h>#include <stdbool.h>

void bar(void){ bool b; if (b) printf("true\n"); if (!b) printf("false\n");}

void foo(void);void bar(void);

int main(void){ foo(); bar();}

void foo(void){ char c = 2; ...}

gcc clang icc

Without optimization...

With optimization...

true false truefalse

false

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 73: Some stuff about C++ and development

example#include <stdio.h>#include <stdbool.h>

void bar(void){ bool b; if (b) printf("true\n"); if (!b) printf("false\n");}

void foo(void);void bar(void);

int main(void){ foo(); bar();}

void foo(void){ char c = 2; ...}

gcc clang icc

Without optimization...

With optimization...

true false truefalse

false

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 74: Some stuff about C++ and development

example#include <stdio.h>#include <stdbool.h>

void bar(void){ bool b; if (b) printf("true\n"); if (!b) printf("false\n");}

void foo(void);void bar(void);

int main(void){ foo(); bar();}

void foo(void){ char c = 2; ...}

gcc clang icc

Without optimization...

With optimization...

true false truefalse

false false

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 75: Some stuff about C++ and development

example#include <stdio.h>#include <stdbool.h>

void bar(void){ bool b; if (b) printf("true\n"); if (!b) printf("false\n");}

void foo(void);void bar(void);

int main(void){ foo(); bar();}

void foo(void){ char c = 2; ...}

gcc clang icc

Without optimization...

With optimization...

true false truefalse

false false

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 76: Some stuff about C++ and development

example#include <stdio.h>#include <stdbool.h>

void bar(void){ bool b; if (b) printf("true\n"); if (!b) printf("false\n");}

void foo(void);void bar(void);

int main(void){ foo(); bar();}

void foo(void){ char c = 2; ...}

gcc clang icc

Without optimization...

With optimization...

true false truefalse

false false false

http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf

Page 77: Some stuff about C++ and development
Page 78: Some stuff about C++ and development

why do cars have brakes?

Page 79: Some stuff about C++ and development

so you can drive faster!

Page 80: Some stuff about C++ and development

do you do unit testing?

Page 81: Some stuff about C++ and development

what do you mean by the word

unit ?

Page 82: Some stuff about C++ and development

it's nothing to do with size

Page 83: Some stuff about C++ and development

A

tests

Page 84: Some stuff about C++ and development

A

tests

B

Page 85: Some stuff about C++ and development

A

tests

C

B

Page 86: Some stuff about C++ and development

A

tests

C

B

D

Page 87: Some stuff about C++ and development

A

tests

C

B

D

E

Page 88: Some stuff about C++ and development

A

tests

C

B

F

D

E

Page 89: Some stuff about C++ and development

A

tests

C

B

FG

D

E

Page 90: Some stuff about C++ and development

A

tests

C

B

FG

D

E

extern

al boundary

Page 91: Some stuff about C++ and development

A

tests

C

B

FG

D

database

extern

al boundary

Page 92: Some stuff about C++ and development

A

tests

C

B

Fregistry

D

database

extern

al boundary

Page 93: Some stuff about C++ and development

A

tests

C

B

Fregistry

D

file system

extern

al boundary

Page 94: Some stuff about C++ and development

A

tests

C

B

Fsocket

D

file system

extern

al boundary

Page 95: Some stuff about C++ and development

A

tests

C

B

F

socket

D

file system

extern

al boundary

unit tests should pass or fail based solely on the correctness of our tests

and our code

Page 96: Some stuff about C++ and development

A

tests

C

B

F

socket

D

file system

extern

al boundary

not based on the correctness of external code and its environment

Page 97: Some stuff about C++ and development

this means we have to design "seams"

into our architecture

Page 98: Some stuff about C++ and development

A

tests

C

B

F

socket

D

file system

substitutefile system

substitutesocket

seam

seam

Page 99: Some stuff about C++ and development

this is a useful

definition

Page 100: Some stuff about C++ and development

it means unit tests

are...

Page 101: Some stuff about C++ and development
Page 102: Some stuff about C++ and development
Page 103: Some stuff about C++ and development

so fast, your basic development

activity can change from...

Page 104: Some stuff about C++ and development

re-compiling

Page 105: Some stuff about C++ and development

to...

Page 106: Some stuff about C++ and development

re-running the unit tests

Page 107: Some stuff about C++ and development

this is not a mere quantitative

change

Page 108: Some stuff about C++ and development

it is a qualitative change

Page 109: Some stuff about C++ and development

some more about tests being...

Page 110: Some stuff about C++ and development

void example_of_use(){ q = snafu::instance().query(...) ... snafu::instance().modifier(...); ...}

spot the anti-pattern

Page 111: Some stuff about C++ and development

class snafu {public: // singleton static snafu & instance(); public: // api int query(...) const; void modifier(...);

private: // inappropriate snafu(const snafu &); snafu & operator=(const snafu &); private: // 'tors snafu(); ~snafu(); };

singleton

Page 112: Some stuff about C++ and development

this is the only good singleton

Page 113: Some stuff about C++ and development

A

tests

C

B

F

socket

D

file system

substitutefile system

substitutesocket

seam

seam

1

singleton

Page 114: Some stuff about C++ and development

A

unittest

C

B

F

D

1

danger

Page 115: Some stuff about C++ and development

A

unittest

C

B

F

D

1

anotherunittest

danger

Page 116: Some stuff about C++ and development

you should be able to run your

unit tests in any order

Page 117: Some stuff about C++ and development

you should be able to run your

unit tests in parallel

Page 118: Some stuff about C++ and development

unit tests

integration

system

no externaldependencies

some externaldependencies

all externaldependencies

Page 119: Some stuff about C++ and development

waterfall

anal

ysis

desi

gn

impl

emen

t

test

Page 120: Some stuff about C++ and development

waterfall

anal

ysis

desi

gn

impl

emen

t

debu

g

Page 121: Some stuff about C++ and development

Debugging is twice as hard as writing the code in the first

place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart

enough to debug it.

Page 122: Some stuff about C++ and development

"agile"... ... ...test ... ... ...test ... ... ...test

Page 123: Some stuff about C++ and development
Page 124: Some stuff about C++ and development

virtual destructor?

class dice_roller{public: virtual ~dice_roller(); ... virtual int roll_dice() const = 0;};

question:what does a virtual destructor mean?

Page 125: Some stuff about C++ and development

virtual destructor

class random_dice_roller : public dice_roller{public: ...};

answer:any code can delete a derived object through a base class pointers*

void contrived_example(){ dice_roller * ptr = new random_dice_roller(); ... delete ptr;}

* and it will work!

Page 126: Some stuff about C++ and development

virtual destructor

class random_dice_roller : public dice_roller{public: ...};

answer:any code can delete a derived object through a base class pointers*

void contrived_example(){ dice_roller * ptr = new random_dice_roller(); ... delete ptr;}

* and it will work!

Page 127: Some stuff about C++ and development

virtual destructor?

class random_dice_roller : public dice_roller{public: ...};

question:should any code be able to write delete expressions?

void contrived_example(){ dice_roller * ptr = new random_dice_roller(); ... delete ptr;}

class dice_roller{public: virtual ~dice_roller(); ...};

Page 128: Some stuff about C++ and development

virtual destructor

suppose you don't want arbitrary code to be able to write delete expressions?

void contrived_example(){ dice_roller * ptr = new random_dice_roller(); ... delete ptr;}

class dice_roller{public: ...protected: ~dice_roller();};

Page 129: Some stuff about C++ and development
Page 130: Some stuff about C++ and development

bad inheritance

alpha

beta

gamma

delta

new alpha()

new beta()

new gamma()

new delta()

Page 131: Some stuff about C++ and development

good inheritance

alpha beta gamma

abstraction

delta

client

Page 132: Some stuff about C++ and development

factory pattern

abstractfactory

abstractproduct

concreteproduct

concretefactory

<<creates>>

<<creates>>

client

Page 133: Some stuff about C++ and development
Page 134: Some stuff about C++ and development

#include header

#include "wibble.hpp"

class fubar {

};

Page 135: Some stuff about C++ and development

inheritance

#include "wibble.hpp"

class fubar : public wibble // 1{

};

Page 136: Some stuff about C++ and development

member function parameters

#include "wibble.hpp"

class fubar : public wibble // 1{ void value_parameter(wibble ); // 2 void ref_parameter(wibble &); // 3 void ptr_parameter(wibble *); // 4

};

Page 137: Some stuff about C++ and development

member function return types

#include "wibble.hpp"

class fubar : public wibble // 1{ void value_parameter(wibble ); // 2 void ref_parameter(wibble &); // 3 void ptr_parameter(wibble *); // 4

wibble value_result(); // 5 wibble & ref_result(); // 6 wibble * ptr_result(); // 7

};

Page 138: Some stuff about C++ and development

data members

#include "wibble.hpp"

class fubar : public wibble // 1{ void value_parameter(wibble ); // 2 void ref_parameter(wibble &); // 3 void ptr_parameter(wibble *); // 4

wibble value_result(); // 5 wibble & ref_result(); // 6 wibble * ptr_result(); // 7

wibble value; // 8 wibble & ref; // 9 wibble * ptr; // 10

};

Page 139: Some stuff about C++ and development

static data member#include "wibble.hpp"

class fubar : public wibble // 1{ void value_parameter(wibble ); // 2 void ref_parameter(wibble &); // 3 void ptr_parameter(wibble *); // 4

wibble value_result(); // 5 wibble & ref_result(); // 6 wibble * ptr_result(); // 7

wibble value; // 8 wibble & ref; // 9 wibble * ptr; // 10

static wibble shared_value; // 11 static wibble & shared_ref; // 12 static wibble * shared_ptr; // 13};

Page 140: Some stuff about C++ and development

forward declarationclass wibble;

class fubar : public wibble // 1{ void value_parameter(wibble ); // 2 void ref_parameter(wibble &); // 3 void ptr_parameter(wibble *); // 4

wibble value_result(); // 5 wibble & ref_result(); // 6 wibble * ptr_result(); // 7

wibble value; // 8 wibble & ref; // 9 wibble * ptr; // 10

static wibble shared_value; // 11 static wibble & shared_ref; // 12 static wibble * shared_ptr; // 13};

Page 141: Some stuff about C++ and development

which of 1..13 require #include ?#include "wibble.hpp"

class fubar : public wibble // 1{ void value_parameter(wibble ); // 2 void ref_parameter(wibble &); // 3 void ptr_parameter(wibble *); // 4

wibble value_result(); // 5 wibble & ref_result(); // 6 wibble * ptr_result(); // 7

wibble value; // 8 wibble & ref; // 9 wibble * ptr; // 10

static wibble shared_value; // 11 static wibble & shared_ref; // 12 static wibble * shared_ptr; // 13};

Page 142: Some stuff about C++ and development

my assistant will now collect up the

answers

Page 143: Some stuff about C++ and development

the answer is...

class wibble;

class fubar : public wibble // 1{ void value_parameter(wibble ); // 2 void ref_parameter(wibble &); // 3 void ptr_parameter(wibble *); // 4

wibble value_result(); // 5 wibble & ref_result(); // 6 wibble * ptr_result(); // 7

wibble value; // 8 wibble & ref; // 9 wibble * ptr; // 10

static wibble shared_value; // 11 static wibble & shared_ptr; // 12 static wibble * shared_ptr; // 13};

Page 144: Some stuff about C++ and development

we'll see how you all did shortly!

Page 145: Some stuff about C++ and development

#include in .hpp .cpp

#ifndef FUBAR_INCLUDED#define FUBAR_INCLUDED

#include "wibble.hpp"#include "snafu.hpp"#include "widget.hpp"

class fubar { ...};

#endif

#include "fubar.hpp"

...

fubar.hpp client.cpp

Page 146: Some stuff about C++ and development

#ifndef FUBAR_INCLUDED#define FUBAR_INCLUDED

#include "wibble.hpp"

class snafu;class widget;

class fubar { ...};

#endif

#include "fubar.hpp"#include "snafu.hpp"#include "widget.hpp"

...

fubar.hpp client.cpp

• compile times down• physical coupling down• happiness up

#include in .hpp .cpp

Page 147: Some stuff about C++ and development

how much does a #include cost?

#include <string>

includer.cpp

$ g++ -H includer.cpp &> lines$ wc -l lines

Page 148: Some stuff about C++ and development

how much does a #include cost?

#include <string>

includer.cpp

$ g++ -H includer.cpp &> lines$ wc -l lines

. /usr/include/c++/4.2.1/string

.. /usr/include/c++/4.2.1/bits/c++config.h

... /usr/include/c++/4.2.1/bits/os_defines.h

.... /usr/include/unistd.h

..... /usr/include/_types.h

...... /usr/include/sys/_types.h

....... /usr/include/sys/cdefs.h

........ /usr/include/sys/_symbol_aliasing.h

........ /usr/include/sys/_posix_availability.h

....... /usr/include/machine/_types.h

........ /usr/include/i386/_types.h

..... /usr/include/sys/unistd.h

...... /usr/include/sys/cdefs.h ... ... ...... /usr/include/c++/4.2.1/bits/stl_uninitialized.h... /usr/include/c++/4.2.1/bits/stl_algo.h.... /usr/include/c++/4.2.1/bits/stl_heap.h..... /usr/include/c++/4.2.1/debug/debug.h.... /usr/include/c++/4.2.1/bits/stl_tempbuf.h..... /usr/include/c++/4.2.1/memory.... /usr/include/c++/4.2.1/debug/debug.h.. /usr/include/c++/4.2.1/bits/basic_string.tcc

Page 149: Some stuff about C++ and development

how much does a #include cost?

#include <string>

includer.cpp

$ g++ -H includer.cpp &> lines$ wc -l lines

. /usr/include/c++/4.2.1/string

.. /usr/include/c++/4.2.1/bits/c++config.h

... /usr/include/c++/4.2.1/bits/os_defines.h

.... /usr/include/unistd.h

..... /usr/include/_types.h

...... /usr/include/sys/_types.h

....... /usr/include/sys/cdefs.h

........ /usr/include/sys/_symbol_aliasing.h

........ /usr/include/sys/_posix_availability.h

....... /usr/include/machine/_types.h

........ /usr/include/i386/_types.h

..... /usr/include/sys/unistd.h

...... /usr/include/sys/cdefs.h ... ... ...... /usr/include/c++/4.2.1/bits/stl_uninitialized.h... /usr/include/c++/4.2.1/bits/stl_algo.h.... /usr/include/c++/4.2.1/bits/stl_heap.h..... /usr/include/c++/4.2.1/debug/debug.h.... /usr/include/c++/4.2.1/bits/stl_tempbuf.h..... /usr/include/c++/4.2.1/memory.... /usr/include/c++/4.2.1/debug/debug.h.. /usr/include/c++/4.2.1/bits/basic_string.tcc

$ g++ -H includer.cpp &> lines$ wc -l lines 244 lines

Page 150: Some stuff about C++ and development

you must not tell lies!

#ifndef FUBAR_INCLUDED#define FUBAR_INCLUDED

#include "wibble.hpp"

class snafu;class widget;

class fubar { ...};

#endif

namespace fishing{ class snafu { ... };}

fubar.hppfishing/snafu.hpp

namespace fishing{ class widget { ... };}

fishing/widget.hpp

Page 151: Some stuff about C++ and development

you must not tell lies!

#ifndef FUBAR_INCLUDED#define FUBAR_INCLUDED

#include "wibble.hpp"

namespace fishing{ class snafu; class widget;}

class fubar { ...};

#endif

fubar.hppnamespace fishing{ class snafu { ... };}

fishing/snafu.hpp

namespace fishing{ class widget { ... };}

fishing/widget.hpp

Page 152: Some stuff about C++ and development

you must not tell lies!

#ifndef FUBAR_INCLUDED#define FUBAR_INCLUDED

#include "wibble.hpp"

namespace std{ class string;}

class fubar { ...};

#endif

namespace std{ class string { ... };}

fubar.hpp <string>

it's not like this!

Page 153: Some stuff about C++ and development

you must not tell lies!

#ifndef FUBAR_INCLUDED#define FUBAR_INCLUDED

#include "wibble.hpp"

namespace std{ class string;}

class fubar { ...};

#endif

namespace std{ template<typename T> class basic_string { ... };

typedef basic_string<char> string; ...

}

fubar.hpp <string>

it's a bit like this!

Page 154: Some stuff about C++ and development

you must not tell lies!

#ifndef FUBAR_INCLUDED#define FUBAR_INCLUDED

#include "wibble.hpp"

namespace std{ template<typename T> class basic_string;

typdef basic_string<char> string;}

class fubar { ...};

#endif

namespace std{ template<typename T> class basic_string { ... };

typedef basic_string<char> string; ...

}

fubar.hpp <string>

??

Page 155: Some stuff about C++ and development

you must not tell lies!

#ifndef FUBAR_INCLUDED#define FUBAR_INCLUDED

#include "wibble.hpp"

namespace std{ template<typename T> class basic_string;

typdef basic_string<char> string;}

class fubar{ ...};

#endif

namespace std{ template<typename T1, typename T2 = ..., typename T3 = ...> class basic_string { ... };

typedef basic_string<char> string; ...

}

fubar.hpp <string>default template types

Page 156: Some stuff about C++ and development

don't forward declare anything in std::

Page 157: Some stuff about C++ and development

template

#ifndef SNAFU_INCLUDED#define SNAFU_INCLUDED

#include <algorithm>...

template<typename T>class snafu{public: void reserve(int limit) { std::for_each(...) } ...};

#endif

snafu.hpp

templates canincreasecoupling

Page 158: Some stuff about C++ and development

template

#ifndef SNAFU_INCLUDED#define SNAFU_INCLUDED

...

template<typename T>class snafu{public: void reserve(int limit); ...};

#include "snafu-template.hpp"

#endif

#include <algorithm>

template<typename T>void snafu<T>::reserve(int limit){ std::for_each(...)}

...

snafu.hpp snafu-template.hpp

templates canincreasecoupling

Page 159: Some stuff about C++ and development

inline

#ifndef FUBAR_INCLUDED#define FUBAR_INCLUDED

#include <vector>

class fubar{public: typedef std::vector<int>::iterator iterator;

void algo(iterator from, iterator to) { ... } ...};

inlining usually increases coupling

fubar.hpp

Page 160: Some stuff about C++ and development

template

#ifndef FUBAR_INCLUDED#define FUBAR_INCLUDED

...

class fubar{public: template<typename iterator> void algo(iterator from, iterator to) { ... } ...};

#endif

#include "fubar.hpp"#include <vector>

void eg(std::vector<int> & v){ fubar f; f.algo(v.begin(), v.end());}

fubar.hppexample.cpp

templates canalso decrease

coupling!

Page 161: Some stuff about C++ and development
Page 162: Some stuff about C++ and development

does this compile?

#ifndef FUBAR_INCLUDED#define FUBAR_INCLUDED

#include "wibble.hpp"

class fubar{public: ... void fu(); ...private: std::vector<wibble> wibbles;};

#endif

#include <vector>#include "fubar.hpp"

void fubar::fu(){ ...}

fubar.hpp fubar.cpp

Page 163: Some stuff about C++ and development

does this compile?

#ifndef FUBAR_INCLUDED#define FUBAR_INCLUDED

#include "wibble.hpp"

class fubar{public: ... void fu(); ...private: std::vector<wibble> wibbles;};

#endif

#include <vector>#include "fubar.hpp"

void fubar::fu(){ ...}

fubar.hpp fubar.cpp

yes

Page 164: Some stuff about C++ and development

does this compile?

#ifndef FUBAR_INCLUDED#define FUBAR_INCLUDED

#include "wibble.hpp"

class fubar{public: ... void fu(); ...private: std::vector<wibble> wibbles;};

#endif

#include "fubar.hpp"#include <vector>

void fubar::fu(){ ...}

fubar.hpp fubar.cpp

Page 165: Some stuff about C++ and development

does this compile?

#ifndef FUBAR_INCLUDED#define FUBAR_INCLUDED

#include "wibble.hpp"

class fubar{public: ... void fu(); ...private: std::vector<wibble> wibbles;};

#endif

#include "fubar.hpp"#include <vector>

void fubar::fu(){ ...}

fubar.hpp fubar.cpp

no

Page 166: Some stuff about C++ and development

#ifndef FUBAR_INCLUDED#define FUBAR_INCLUDED

#include "wibble.hpp"

class fubar{public: ... void fu(); ...private: std::vector<wibble> wibbles;};

#endif

#include "fubar.hpp"#include <vector>

void fubar::fu(){ ...}

fubar.hpp fubar.cpp

Page 167: Some stuff about C++ and development

does this compile?

#ifndef FUBAR_INCLUDED#define FUBAR_INCLUDED

#include "wibble.hpp"#include <vector>

class fubar{public: ... void fu(); ...private: std::vector<wibble> wibbles;};

#endif

#include "fubar.hpp"...

void fubar::fu(){ ...}

fubar.hpp fubar.cpp

Page 168: Some stuff about C++ and development

does this compile?

#ifndef FUBAR_INCLUDED#define FUBAR_INCLUDED

#include "wibble.hpp"#include <vector>

class fubar{public: ... void fu(); ...private: std::vector<wibble> wibbles;};

#endif

#include "fubar.hpp"...

void fubar::fu(){ ...}

fubar.hpp fubar.cpp

yes

Page 169: Some stuff about C++ and development

include your own header 1st

#ifndef FUBAR_INCLUDED#define FUBAR_INCLUDED

#include "wibble.hpp"#include <vector>

class fubar{public: ... void fu(); ...private: std::vector<wibble> wibbles;};

#endif

#include "fubar.hpp"...

void fubar::fu(){ ...}

fubar.hpp fubar.cpp

Page 170: Some stuff about C++ and development

better still - make sure each header compiles (on its own) as

part of the build!

#ifndef FUBAR_INCLUDED#define FUBAR_INCLUDED

#include "wibble.hpp"#include <vector>

class fubar{public: ... void fu(); ...private: std::vector<wibble> wibbles;};

#endif

fubar.hpp

Page 171: Some stuff about C++ and development

remember this...

Page 172: Some stuff about C++ and development

#include "wibble.hpp"

class fubar : public wibble // 1{ void value_parameter(wibble ); // 2 void ref_parameter(wibble &); // 3 void ptr_parameter(wibble *); // 4

wibble value_result(); // 5 wibble & ref_result(); // 6 wibble * ptr_result(); // 7

wibble value; // 8 wibble & ref; // 9 wibble * ptr; // 10

static wibble shared_value; // 11};

Page 173: Some stuff about C++ and development

the answer is...

#include "wibble.hpp"

class fubar : public wibble // 1{ void value_parameter(wibble ); // 2 void ref_parameter(wibble &); // 3 void ptr_parameter(wibble *); // 4

wibble value_result(); // 5 wibble & ref_result(); // 6 wibble * ptr_result(); // 7

wibble value; // 8 wibble & ref; // 9 wibble * ptr; // 10

static wibble shared_value; // 11};

Page 174: Some stuff about C++ and development

how did you all do?

Page 175: Some stuff about C++ and development

over to my assistant...

Page 176: Some stuff about C++ and development

the Satir change curve

old status quo

new status quo old status quo

foreign element

resistance and chaosin

tegr

atio

n an

d pr

acti

cetime

com

pete

nce