SlideShare a Scribd company logo
1 of 177
Download to read offline
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.
sequence points
program state
side effects
time
sequence points
program state
side effects
time
sequence points
program state
side effects
time
sequence points
program state
side effects
time
sequence points
program state
side effects
time
sequence points
program state
side effects
time
sequence points
program state
side effects
time
sequence points
program state
side effects
time
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
sequence points
In reality C and C++ have
very few sequence points
(this is to give maximum
scope for optimization).
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
only
sequence points
govern the order of
evaluation
got it?
sequence points
i = v[++i] + v[++i];
question: where are the
sequence points
in this statement ?
sequence points
i = v[++i] + v[++i];
answer: here!
why does
this matter?
I'm glad
you asked!
here's
why...
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
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
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
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
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
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("%dn", n);
}
$ gcc foo.c && ./a.out
gcc
(Mac OS 10.8.2)
http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
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("%dn", n);
}
$ gcc foo.c && ./a.out
gcc
(Mac OS 10.8.2)
$ gcc foo.c && ./a.out
12
http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
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("%dn", n);
}
$ gcc foo.c && ./a.out
gcc
$ clang foo.c && ./a.out
clang
(Mac OS 10.8.2)
$ gcc foo.c && ./a.out
12
http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
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("%dn", n);
}
$ gcc foo.c && ./a.out
gcc
$ clang foo.c && ./a.out
clang
(Mac OS 10.8.2)
$ gcc foo.c && ./a.out
12
$ clang foo.c && ./a.out
11
http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
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("%dn", 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.out
12
$ clang foo.c && ./a.out
11
http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
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("%dn", 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.out
12
$ clang foo.c && ./a.out
11
$ icc foo.c && ./a.out
13
http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
a reference to
a[i]
can also be written as
*(a+i)
5.3 Pointers and Arrays
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 %dn",
dst[0], dst[1],
dst[2], dst[3]);
}
$ gcc *.c && ./a.out
gcc 4.8.0 20130210
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 %dn",
dst[0], dst[1],
dst[2], dst[3]);
}
$ gcc *.c && ./a.out$ gcc *.c && ./a.out
0 2 0 0
gcc 4.8.0 20130210
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 %dn",
dst[0], dst[1],
dst[2], dst[3]);
}
$ gcc *.c && ./a.out$ gcc *.c && ./a.out
0 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 %dn",
dst[0], dst[1],
dst[2], dst[3]);
}
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 %dn",
dst[0], dst[1],
dst[2], dst[3]);
}
$ gcc *.c && ./a.out $ gcc *.c && ./a.out$ gcc *.c && ./a.out
0 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 %dn",
dst[0], dst[1],
dst[2], dst[3]);
}
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 %dn",
dst[0], dst[1],
dst[2], dst[3]);
}
$ gcc *.c && ./a.out $ gcc *.c && ./a.out$ gcc *.c && ./a.out
0 2 0 0
$ gcc *.c && ./a.out
0 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 %dn",
dst[0], dst[1],
dst[2], dst[3]);
}
precedence is not the
same as order of
evaluation
order of evaluation is
mostly unspecified
and is a
run-time thing
precedence is
completely specified
and is a
compile-time thing
precedence determines
which operands
bind to
which operators
a * b + c
example
a * b + c
?
a * b + c
?
a() * b() + c()
temp_a = a();
temp_b = b();
temp_c = c();
a * b + c
temp_c = c();
temp_b = b();
temp_a = a();
a * b + c
temp_b = b();
temp_c = c();
temp_a = a();
a * b + c
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
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
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.out
ab7
http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
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.out
ab7
http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
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.out
ab7
$ g++ *.c && ./a.out
ba7
http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
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.
example
#include <stdio.h>
#include <stdbool.h>
void bar(void)
{
bool b;
if (b)
printf("truen");
if (!b)
printf("falsen");
}
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
example
#include <stdio.h>
#include <stdbool.h>
void bar(void)
{
bool b;
if (b)
printf("truen");
if (!b)
printf("falsen");
}
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
example
#include <stdio.h>
#include <stdbool.h>
void bar(void)
{
bool b;
if (b)
printf("truen");
if (!b)
printf("falsen");
}
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
example
#include <stdio.h>
#include <stdbool.h>
void bar(void)
{
bool b;
if (b)
printf("truen");
if (!b)
printf("falsen");
}
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
example
#include <stdio.h>
#include <stdbool.h>
void bar(void)
{
bool b;
if (b)
printf("truen");
if (!b)
printf("falsen");
}
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
example
#include <stdio.h>
#include <stdbool.h>
void bar(void)
{
bool b;
if (b)
printf("truen");
if (!b)
printf("falsen");
}
void foo(void);
void bar(void);
int main(void)
{
foo();
bar();
}
void foo(void)
{
char c = 2;
...
}
gcc clang icc
Without optimization...
true false true
false
http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
example
#include <stdio.h>
#include <stdbool.h>
void bar(void)
{
bool b;
if (b)
printf("truen");
if (!b)
printf("falsen");
}
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 true
false
http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
example
#include <stdio.h>
#include <stdbool.h>
void bar(void)
{
bool b;
if (b)
printf("truen");
if (!b)
printf("falsen");
}
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 true
false
false
http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
example
#include <stdio.h>
#include <stdbool.h>
void bar(void)
{
bool b;
if (b)
printf("truen");
if (!b)
printf("falsen");
}
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 true
false
false
http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
example
#include <stdio.h>
#include <stdbool.h>
void bar(void)
{
bool b;
if (b)
printf("truen");
if (!b)
printf("falsen");
}
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 true
false
false false
http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
example
#include <stdio.h>
#include <stdbool.h>
void bar(void)
{
bool b;
if (b)
printf("truen");
if (!b)
printf("falsen");
}
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 true
false
false false
http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
example
#include <stdio.h>
#include <stdbool.h>
void bar(void)
{
bool b;
if (b)
printf("truen");
if (!b)
printf("falsen");
}
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 true
false
false false false
http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
why do cars have brakes?
so you can drive faster!
do you do unit
testing?
what do you mean
by the word
unit ?
it's nothing to do
with size
A
tests
A
tests
B
A
tests
C
B
A
tests
C
B
D
A
tests
C
B
D
E
A
tests
C
B
F
D
E
A
tests
C
B
F
G
D
E
A
tests
C
B
F
G
D
E
external boundary
A
tests
C
B
F
G
D
database
external boundary
A
tests
C
B
F
registry
D
database
external boundary
A
tests
C
B
F
registry
D
file system
external boundary
A
tests
C
B
F
socket
D
file system
external boundary
A
tests
C
B
F
socket
D
file system
external boundary
unit tests should pass or fail based
solely on the correctness of our tests
and our code
A
tests
C
B
F
socket
D
file system
external boundary
not based on the correctness of
external code and its environment
this means we have
to design "seams"
into our
architecture
A
tests
C
B
F
socket
D
file system
substitute
file system
substitute
socket
seam
seam
this is a
useful
definition
it means
unit tests
are...
so fast, your basic
development
activity can change
from...
re-compiling
to...
re-running
the unit tests
this is not a mere
quantitative
change
it is a
qualitative
change
some more about tests being...
void example_of_use()
{
q = snafu::instance().query(...)
...
snafu::instance().modifier(...);
...
}
spot the anti-pattern
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
this is the only good singleton
A
tests
C
B
F
socket
D
file system
substitute
file system
substitute
socket
seam
seam
1
singleton
A
unit
test
C
B
F
D
1
danger
A
unit
test
C
B
F
D
1
another
unit
test
danger
you should be
able to run your
unit tests in
any order
you should be
able to run your
unit tests in
parallel
unit tests
integration
system
no external
dependencies
some external
dependencies
all external
dependencies
waterfall
analysis
design
implement
test
waterfall
analysis
design
implement
debug
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.
"agile"
...
...
...
test
...
...
...
test
...
...
...
test
virtual destructor?
class dice_roller
{
public:
virtual ~dice_roller();
...
virtual int roll_dice() const = 0;
};
question:
what does a virtual destructor mean?
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!
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!
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();
...
};
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();
};
bad inheritance
alpha
beta
gamma
delta
new alpha()
new beta()
new gamma()
new delta()
good inheritance
alpha beta gamma
abstraction
delta
client
factory pattern
abstract
factory
abstract
product
concrete
product
concrete
factory
<<creates>>
<<creates>>
client
#include header
#include "wibble.hpp"
class fubar
{
};
inheritance
#include "wibble.hpp"
class fubar : public wibble // 1
{
};
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
};
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
};
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
};
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
};
forward declaration
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_ref; // 12
static wibble * shared_ptr; // 13
};
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
};
my assistant will
now collect up the
answers
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
};
we'll see how you all
did shortly!
#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
#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
how much does a #include cost?
#include <string>
includer.cpp
$ g++ -H includer.cpp &> lines
$ wc -l lines
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
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
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.hpp
fishing/snafu.hpp
namespace fishing
{
class widget
{
...
};
}
fishing/widget.hpp
you must not tell lies!
#ifndef FUBAR_INCLUDED
#define FUBAR_INCLUDED
#include "wibble.hpp"
namespace fishing
{
class snafu;
class widget;
}
class fubar
{
...
};
#endif
fubar.hpp
namespace fishing
{
class snafu
{
...
};
}
fishing/snafu.hpp
namespace fishing
{
class widget
{
...
};
}
fishing/widget.hpp
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!
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!
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>
?
?
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
don't forward declare
anything in std::
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 can
increase
coupling
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 can
increase
coupling
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
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.hpp
example.cpp
templates can
also decrease
coupling!
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
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
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
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
#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
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
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
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
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
remember
this...
#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
};
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
};
how did you
all do?
over to my
assistant...
the Satir change curve
old status quo
new status quo old status quo
foreign element
resistance
and
chaos integration
and
practicetime
competence
@JonJagger
jon@jaggersoft.com

More Related Content

What's hot

Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project FileDeyvessh kumar
 
Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphismmohamed sikander
 
UGC-NET, GATE and all IT Companies Interview C++ Solved Questions PART - 2
UGC-NET, GATE and all IT Companies Interview C++ Solved Questions PART - 2UGC-NET, GATE and all IT Companies Interview C++ Solved Questions PART - 2
UGC-NET, GATE and all IT Companies Interview C++ Solved Questions PART - 2Knowledge Center Computer
 
การเขียนคำสั่งควบคุมขั้นพื้นฐาน 2
การเขียนคำสั่งควบคุมขั้นพื้นฐาน 2การเขียนคำสั่งควบคุมขั้นพื้นฐาน 2
การเขียนคำสั่งควบคุมขั้นพื้นฐาน 2Tay Atcharawan
 
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORYCS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORYRadha Maruthiyan
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manualSyed Mustafa
 
Writing good std::future&lt;c++>
Writing good std::future&lt;c++>Writing good std::future&lt;c++>
Writing good std::future&lt;c++>Anton Bikineev
 
Computer Networks Lab File
Computer Networks Lab FileComputer Networks Lab File
Computer Networks Lab FileKandarp Tiwari
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTsKevlin Henney
 
Quality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStormQuality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStormMichelangelo van Dam
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rathSANTOSH RATH
 

What's hot (20)

Static and const members
Static and const membersStatic and const members
Static and const members
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project File
 
Cquestions
Cquestions Cquestions
Cquestions
 
Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphism
 
Part - 2 Cpp programming Solved MCQ
Part - 2  Cpp programming Solved MCQ Part - 2  Cpp programming Solved MCQ
Part - 2 Cpp programming Solved MCQ
 
UGC-NET, GATE and all IT Companies Interview C++ Solved Questions PART - 2
UGC-NET, GATE and all IT Companies Interview C++ Solved Questions PART - 2UGC-NET, GATE and all IT Companies Interview C++ Solved Questions PART - 2
UGC-NET, GATE and all IT Companies Interview C++ Solved Questions PART - 2
 
การเขียนคำสั่งควบคุมขั้นพื้นฐาน 2
การเขียนคำสั่งควบคุมขั้นพื้นฐาน 2การเขียนคำสั่งควบคุมขั้นพื้นฐาน 2
การเขียนคำสั่งควบคุมขั้นพื้นฐาน 2
 
C lab excellent
C lab excellentC lab excellent
C lab excellent
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORYCS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
 
Implementing stack
Implementing stackImplementing stack
Implementing stack
 
Writing good std::future&lt;c++>
Writing good std::future&lt;c++>Writing good std::future&lt;c++>
Writing good std::future&lt;c++>
 
Computer Networks Lab File
Computer Networks Lab FileComputer Networks Lab File
Computer Networks Lab File
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTs
 
C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
 
Quality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStormQuality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStorm
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
C programms
C programmsC programms
C programms
 

Similar to Some stuff about C++ and development

Fundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan KumariFundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan KumariTHE NORTHCAP UNIVERSITY
 
Lab Manual IV (1).pdf on C++ Programming practice
Lab Manual IV (1).pdf on C++ Programming practiceLab Manual IV (1).pdf on C++ Programming practice
Lab Manual IV (1).pdf on C++ Programming practiceranaibrahim453
 
1 introduction to c program
1 introduction to c program1 introduction to c program
1 introduction to c programNishmaNJ
 
ParallelProgrammingBasics_v2.pdf
ParallelProgrammingBasics_v2.pdfParallelProgrammingBasics_v2.pdf
ParallelProgrammingBasics_v2.pdfChen-Hung Hu
 
Understand more about C
Understand more about CUnderstand more about C
Understand more about CYi-Hsiu Hsu
 
Session05 iteration structure
Session05 iteration structureSession05 iteration structure
Session05 iteration structureHarithaRanasinghe
 
C++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical ReviewerC++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical ReviewerAndrey Karpov
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Languagesatvirsandhu9
 
Chapter i(introduction to java)
Chapter i(introduction to java)Chapter i(introduction to java)
Chapter i(introduction to java)Chhom Karath
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstractionIntro C# Book
 

Similar to Some stuff about C++ and development (20)

Fundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan KumariFundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan Kumari
 
UNIT 2 LOOP CONTROL.pptx
UNIT 2 LOOP CONTROL.pptxUNIT 2 LOOP CONTROL.pptx
UNIT 2 LOOP CONTROL.pptx
 
175035 cse lab-05
175035 cse lab-05 175035 cse lab-05
175035 cse lab-05
 
Lab Manual IV (1).pdf on C++ Programming practice
Lab Manual IV (1).pdf on C++ Programming practiceLab Manual IV (1).pdf on C++ Programming practice
Lab Manual IV (1).pdf on C++ Programming practice
 
Introduction to c part 2
Introduction to c   part  2Introduction to c   part  2
Introduction to c part 2
 
1 introduction to c program
1 introduction to c program1 introduction to c program
1 introduction to c program
 
ParallelProgrammingBasics_v2.pdf
ParallelProgrammingBasics_v2.pdfParallelProgrammingBasics_v2.pdf
ParallelProgrammingBasics_v2.pdf
 
Understand more about C
Understand more about CUnderstand more about C
Understand more about C
 
operators.ppt
operators.pptoperators.ppt
operators.ppt
 
Session05 iteration structure
Session05 iteration structureSession05 iteration structure
Session05 iteration structure
 
pointers 1
pointers 1pointers 1
pointers 1
 
C++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical ReviewerC++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical Reviewer
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
 
901131 examples
901131 examples901131 examples
901131 examples
 
Working with IDE
Working with IDEWorking with IDE
Working with IDE
 
Chapter i(introduction to java)
Chapter i(introduction to java)Chapter i(introduction to java)
Chapter i(introduction to java)
 
Code optimization
Code optimization Code optimization
Code optimization
 
Code optimization
Code optimization Code optimization
Code optimization
 
CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
 

More from Jon Jagger

Design and Evolution of cyber-dojo
Design and Evolution of cyber-dojoDesign and Evolution of cyber-dojo
Design and Evolution of cyber-dojoJon Jagger
 
NorDevCon 2016 pair programming
NorDevCon 2016 pair programmingNorDevCon 2016 pair programming
NorDevCon 2016 pair programmingJon Jagger
 
Oslo Day of Docker Opening Keynote
Oslo Day of Docker Opening KeynoteOslo Day of Docker Opening Keynote
Oslo Day of Docker Opening KeynoteJon Jagger
 
Pair programming
Pair programmingPair programming
Pair programmingJon Jagger
 
Cyber-dojo: How to perform deliberate practice
Cyber-dojo: How to perform deliberate practiceCyber-dojo: How to perform deliberate practice
Cyber-dojo: How to perform deliberate practiceJon Jagger
 
Atlantec - tdd lessons
Atlantec - tdd lessonsAtlantec - tdd lessons
Atlantec - tdd lessonsJon Jagger
 
lessons from testing
lessons from testinglessons from testing
lessons from testingJon Jagger
 
Lessons from Testing
Lessons from TestingLessons from Testing
Lessons from TestingJon Jagger
 
Systems thinking
Systems thinkingSystems thinking
Systems thinkingJon Jagger
 
Kanban Push-me Pull-you
Kanban Push-me Pull-youKanban Push-me Pull-you
Kanban Push-me Pull-youJon Jagger
 
Larry and Jen do Roman Numerals in C++
Larry and Jen do Roman Numerals in C++Larry and Jen do Roman Numerals in C++
Larry and Jen do Roman Numerals in C++Jon Jagger
 
Systems Thinking
Systems ThinkingSystems Thinking
Systems ThinkingJon Jagger
 
An Agile A to Z
An Agile A to ZAn Agile A to Z
An Agile A to ZJon Jagger
 

More from Jon Jagger (13)

Design and Evolution of cyber-dojo
Design and Evolution of cyber-dojoDesign and Evolution of cyber-dojo
Design and Evolution of cyber-dojo
 
NorDevCon 2016 pair programming
NorDevCon 2016 pair programmingNorDevCon 2016 pair programming
NorDevCon 2016 pair programming
 
Oslo Day of Docker Opening Keynote
Oslo Day of Docker Opening KeynoteOslo Day of Docker Opening Keynote
Oslo Day of Docker Opening Keynote
 
Pair programming
Pair programmingPair programming
Pair programming
 
Cyber-dojo: How to perform deliberate practice
Cyber-dojo: How to perform deliberate practiceCyber-dojo: How to perform deliberate practice
Cyber-dojo: How to perform deliberate practice
 
Atlantec - tdd lessons
Atlantec - tdd lessonsAtlantec - tdd lessons
Atlantec - tdd lessons
 
lessons from testing
lessons from testinglessons from testing
lessons from testing
 
Lessons from Testing
Lessons from TestingLessons from Testing
Lessons from Testing
 
Systems thinking
Systems thinkingSystems thinking
Systems thinking
 
Kanban Push-me Pull-you
Kanban Push-me Pull-youKanban Push-me Pull-you
Kanban Push-me Pull-you
 
Larry and Jen do Roman Numerals in C++
Larry and Jen do Roman Numerals in C++Larry and Jen do Roman Numerals in C++
Larry and Jen do Roman Numerals in C++
 
Systems Thinking
Systems ThinkingSystems Thinking
Systems Thinking
 
An Agile A to Z
An Agile A to ZAn Agile A to Z
An Agile A to Z
 

Recently uploaded

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 

Some stuff about C++ and development

  • 1. some stuff about C++ and development
  • 2. 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.
  • 11. 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]
  • 12. 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]
  • 13. 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]
  • 14. 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]
  • 15. 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]
  • 16. 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]
  • 17. 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]
  • 18. 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]
  • 19. 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]
  • 20. 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]
  • 21. sequence points In reality C and C++ have very few sequence points (this is to give maximum scope for optimization).
  • 22. 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
  • 23. only sequence points govern the order of evaluation
  • 25. sequence points i = v[++i] + v[++i]; question: where are the sequence points in this statement ?
  • 26. sequence points i = v[++i] + v[++i]; answer: here!
  • 30. 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
  • 31. 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
  • 32. 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
  • 33. 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
  • 34. 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
  • 35. 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("%dn", n); } $ gcc foo.c && ./a.out gcc (Mac OS 10.8.2) http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
  • 36. 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("%dn", n); } $ gcc foo.c && ./a.out gcc (Mac OS 10.8.2) $ gcc foo.c && ./a.out 12 http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
  • 37. 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("%dn", n); } $ gcc foo.c && ./a.out gcc $ clang foo.c && ./a.out clang (Mac OS 10.8.2) $ gcc foo.c && ./a.out 12 http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
  • 38. 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("%dn", n); } $ gcc foo.c && ./a.out gcc $ clang foo.c && ./a.out clang (Mac OS 10.8.2) $ gcc foo.c && ./a.out 12 $ clang foo.c && ./a.out 11 http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
  • 39. 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("%dn", 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.out 12 $ clang foo.c && ./a.out 11 http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
  • 40. 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("%dn", 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.out 12 $ clang foo.c && ./a.out 11 $ icc foo.c && ./a.out 13 http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
  • 41. a reference to a[i] can also be written as *(a+i) 5.3 Pointers and Arrays
  • 42. 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 %dn", dst[0], dst[1], dst[2], dst[3]); } $ gcc *.c && ./a.out gcc 4.8.0 20130210
  • 43. 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 %dn", dst[0], dst[1], dst[2], dst[3]); } $ gcc *.c && ./a.out$ gcc *.c && ./a.out 0 2 0 0 gcc 4.8.0 20130210
  • 44. 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 %dn", dst[0], dst[1], dst[2], dst[3]); } $ gcc *.c && ./a.out$ gcc *.c && ./a.out 0 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 %dn", dst[0], dst[1], dst[2], dst[3]); }
  • 45. 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 %dn", dst[0], dst[1], dst[2], dst[3]); } $ gcc *.c && ./a.out $ gcc *.c && ./a.out$ gcc *.c && ./a.out 0 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 %dn", dst[0], dst[1], dst[2], dst[3]); }
  • 46. 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 %dn", dst[0], dst[1], dst[2], dst[3]); } $ gcc *.c && ./a.out $ gcc *.c && ./a.out$ gcc *.c && ./a.out 0 2 0 0 $ gcc *.c && ./a.out 0 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 %dn", dst[0], dst[1], dst[2], dst[3]); }
  • 47.
  • 48. precedence is not the same as order of evaluation
  • 49. order of evaluation is mostly unspecified and is a run-time thing
  • 50. precedence is completely specified and is a compile-time thing
  • 52. a * b + c example
  • 53. a * b + c ?
  • 54. a * b + c ?
  • 55. a() * b() + c()
  • 56. temp_a = a(); temp_b = b(); temp_c = c(); a * b + c
  • 57. temp_c = c(); temp_b = b(); temp_a = a(); a * b + c
  • 58. temp_b = b(); temp_c = c(); temp_a = a(); a * b + c
  • 59. 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
  • 60. 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
  • 61. 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.out ab7 http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
  • 62. 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.out ab7 http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
  • 63. 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.out ab7 $ g++ *.c && ./a.out ba7 http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
  • 64. 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.
  • 65. example #include <stdio.h> #include <stdbool.h> void bar(void) { bool b; if (b) printf("truen"); if (!b) printf("falsen"); } 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
  • 66. example #include <stdio.h> #include <stdbool.h> void bar(void) { bool b; if (b) printf("truen"); if (!b) printf("falsen"); } 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
  • 67. example #include <stdio.h> #include <stdbool.h> void bar(void) { bool b; if (b) printf("truen"); if (!b) printf("falsen"); } 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
  • 68. example #include <stdio.h> #include <stdbool.h> void bar(void) { bool b; if (b) printf("truen"); if (!b) printf("falsen"); } 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
  • 69. example #include <stdio.h> #include <stdbool.h> void bar(void) { bool b; if (b) printf("truen"); if (!b) printf("falsen"); } 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
  • 70. example #include <stdio.h> #include <stdbool.h> void bar(void) { bool b; if (b) printf("truen"); if (!b) printf("falsen"); } void foo(void); void bar(void); int main(void) { foo(); bar(); } void foo(void) { char c = 2; ... } gcc clang icc Without optimization... true false true false http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
  • 71. example #include <stdio.h> #include <stdbool.h> void bar(void) { bool b; if (b) printf("truen"); if (!b) printf("falsen"); } 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 true false http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
  • 72. example #include <stdio.h> #include <stdbool.h> void bar(void) { bool b; if (b) printf("truen"); if (!b) printf("falsen"); } 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 true false false http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
  • 73. example #include <stdio.h> #include <stdbool.h> void bar(void) { bool b; if (b) printf("truen"); if (!b) printf("falsen"); } 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 true false false http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
  • 74. example #include <stdio.h> #include <stdbool.h> void bar(void) { bool b; if (b) printf("truen"); if (!b) printf("falsen"); } 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 true false false false http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
  • 75. example #include <stdio.h> #include <stdbool.h> void bar(void) { bool b; if (b) printf("truen"); if (!b) printf("falsen"); } 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 true false false false http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
  • 76. example #include <stdio.h> #include <stdbool.h> void bar(void) { bool b; if (b) printf("truen"); if (!b) printf("falsen"); } 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 true false false false false http://www.pvv.org/~oma/UnspecifiedAndUndefined_ACCU_Apr2013.pdf
  • 77.
  • 78. why do cars have brakes?
  • 79. so you can drive faster!
  • 80. do you do unit testing?
  • 81. what do you mean by the word unit ?
  • 82. it's nothing to do with size
  • 95. A tests C B F socket D file system external boundary unit tests should pass or fail based solely on the correctness of our tests and our code
  • 96. A tests C B F socket D file system external boundary not based on the correctness of external code and its environment
  • 97. this means we have to design "seams" into our architecture
  • 101.
  • 102.
  • 103. so fast, your basic development activity can change from...
  • 105. to...
  • 107. this is not a mere quantitative change
  • 109. some more about tests being...
  • 110. void example_of_use() { q = snafu::instance().query(...) ... snafu::instance().modifier(...); ... } spot the anti-pattern
  • 111. 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
  • 112. this is the only good singleton
  • 116. you should be able to run your unit tests in any order
  • 117. you should be able to run your unit tests in parallel
  • 118. unit tests integration system no external dependencies some external dependencies all external dependencies
  • 121. 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.
  • 123.
  • 124. virtual destructor? class dice_roller { public: virtual ~dice_roller(); ... virtual int roll_dice() const = 0; }; question: what does a virtual destructor mean?
  • 125. 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!
  • 126. 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!
  • 127. 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(); ... };
  • 128. 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(); };
  • 129.
  • 131. good inheritance alpha beta gamma abstraction delta client
  • 133.
  • 135. inheritance #include "wibble.hpp" class fubar : public wibble // 1 { };
  • 136. 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 };
  • 137. 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 };
  • 138. 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 };
  • 139. 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 };
  • 140. forward declaration 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_ref; // 12 static wibble * shared_ptr; // 13 };
  • 141. 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 };
  • 142. my assistant will now collect up the answers
  • 143. 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 };
  • 144. we'll see how you all did shortly!
  • 145. #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
  • 146. #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
  • 147. how much does a #include cost? #include <string> includer.cpp $ g++ -H includer.cpp &> lines $ wc -l lines
  • 148. 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
  • 149. 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
  • 150. 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.hpp fishing/snafu.hpp namespace fishing { class widget { ... }; } fishing/widget.hpp
  • 151. you must not tell lies! #ifndef FUBAR_INCLUDED #define FUBAR_INCLUDED #include "wibble.hpp" namespace fishing { class snafu; class widget; } class fubar { ... }; #endif fubar.hpp namespace fishing { class snafu { ... }; } fishing/snafu.hpp namespace fishing { class widget { ... }; } fishing/widget.hpp
  • 152. 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!
  • 153. 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!
  • 154. 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> ? ?
  • 155. 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
  • 157. 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 can increase coupling
  • 158. 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 can increase coupling
  • 159. 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
  • 160. 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.hpp example.cpp templates can also decrease coupling!
  • 161.
  • 162. 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
  • 163. 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
  • 164. 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
  • 165. 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
  • 166. #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
  • 167. 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
  • 168. 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
  • 169. 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
  • 170. 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
  • 172. #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 };
  • 173. 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 };
  • 176. the Satir change curve old status quo new status quo old status quo foreign element resistance and chaos integration and practicetime competence