c
2012– Andrei Alexandrescu.
1 / 59
Systems Programming & Beyond using C++ and D C++
Andrei Alexandrescu
andrei@erdani.com
Prepared for LASER Summer School 2012
Systems Programming & Beyond using C++ and D C++ Andrei - - PowerPoint PPT Presentation
Systems Programming & Beyond using C++ and D C++ Andrei Alexandrescu andrei@erdani.com Prepared for LASER Summer School 2012 1 / 59 2012 Andrei Alexandrescu. c Prolegomena Assumption: You are intelligent and talented
c
2012– Andrei Alexandrescu.
1 / 59
Andrei Alexandrescu
Prepared for LASER Summer School 2012
c
2012– Andrei Alexandrescu.
3 / 59
impact instead of rote memorization
c
2012– Andrei Alexandrescu.
4 / 59
c
2012– Andrei Alexandrescu.
5 / 59
c
2012– Andrei Alexandrescu.
6 / 59
c
2012– Andrei Alexandrescu.
7 / 59
c
2012– Andrei Alexandrescu.
8 / 59
c
2012– Andrei Alexandrescu.
9 / 59
c
2012– Andrei Alexandrescu.
10 / 59
c
2012– Andrei Alexandrescu.
11 / 59
battle
human ingenuity
c
2012– Andrei Alexandrescu.
12 / 59
c
2012– Andrei Alexandrescu.
13 / 59
schemes
c
2012– Andrei Alexandrescu.
14 / 59
c
2012– Andrei Alexandrescu.
15 / 59
depending on compiler and optimization level
c
2012– Andrei Alexandrescu.
16 / 59
when never used.
c
2012– Andrei Alexandrescu.
17 / 59
c
2012– Andrei Alexandrescu.
18 / 59
c
2012– Andrei Alexandrescu.
18 / 59
c
2012– Andrei Alexandrescu.
19 / 59
c
2012– Andrei Alexandrescu.
19 / 59
c
2012– Andrei Alexandrescu.
20 / 59
c
2012– Andrei Alexandrescu.
20 / 59
c
2012– Andrei Alexandrescu.
21 / 59
c
2012– Andrei Alexandrescu.
22 / 59
c
2012– Andrei Alexandrescu.
23 / 59
c
2012– Andrei Alexandrescu.
24 / 59
c
2012– Andrei Alexandrescu.
25 / 59
c
2012– Andrei Alexandrescu.
26 / 59
c
2012– Andrei Alexandrescu.
27 / 59
c
2012– Andrei Alexandrescu.
28 / 59
c
2012– Andrei Alexandrescu.
29 / 59
(multiwayUnion) intent
sentence
(find)
c
2012– Andrei Alexandrescu.
30 / 59
c
2012– Andrei Alexandrescu.
31 / 59
inside the class
encapsulation
c
2012– Andrei Alexandrescu.
32 / 59
are so 1960s
left-hand argument
c
2012– Andrei Alexandrescu.
33 / 59
c
2012– Andrei Alexandrescu.
34 / 59
c
2012– Andrei Alexandrescu.
35 / 59
instance, many referents
c
2012– Andrei Alexandrescu.
36 / 59
c
2012– Andrei Alexandrescu.
37 / 59
c
2012– Andrei Alexandrescu.
38 / 59
c
2012– Andrei Alexandrescu.
39 / 59
c
2012– Andrei Alexandrescu.
40 / 59
and containers?”
fail
c
2012– Andrei Alexandrescu.
41 / 59
c
2012– Andrei Alexandrescu.
42 / 59
c
2012– Andrei Alexandrescu.
43 / 59
c
2012– Andrei Alexandrescu.
44 / 59
c
2012– Andrei Alexandrescu.
45 / 59
c
2012– Andrei Alexandrescu.
46 / 59
iterators
c
2012– Andrei Alexandrescu.
47 / 59
tenuous
c
2012– Andrei Alexandrescu.
48 / 59
c
2012– Andrei Alexandrescu.
49 / 59
c
2012– Andrei Alexandrescu.
50 / 59
c
2012– Andrei Alexandrescu.
50 / 59
Generic Programming is the endeavor of finding the most abstract expression of a computation without losing its essence or efficiency.
c
2012– Andrei Alexandrescu.
51 / 59
principles
c
2012– Andrei Alexandrescu.
52 / 59
c
2012– Andrei Alexandrescu.
53 / 59
template <class RIt, class V> RIt find(RIt b, RIt e, const V& v) { for (; (e - b) & 3; ++b) { if (*b == v) return b; } for (; b != e; b += 4) { if (*b == v) return b; if (b[1] == v) return b + 1; if (b[2] == v) return b + 2; if (b[3] == v) return b + 3; } }
iterators?
c
2012– Andrei Alexandrescu.
54 / 59
template <class It, class V> It find(It b, It e, const V& v, std::input_iterator_tag) { for (; e != b; ++b) { if (*b == v) return b; } }
c
2012– Andrei Alexandrescu.
55 / 59
template <class It, class V> It find(It b, It e, const V& v, std::random_access_iterator_tag) { for (; (e - b) & 3; ++b) { if (*b == v) return b; } for (; b != e; b += 4) { if (*b == v) return b; if (b[1] == v) return b + 1; if (b[2] == v) return b + 2; if (b[3] == v) return b + 3; } }
c
2012– Andrei Alexandrescu.
56 / 59
template <class It, class V> It find(It b, It e, const V& v) { return find(b, e, v, typename iterator_traits<It>::iterator_category()); }
c
2012– Andrei Alexandrescu.
57 / 59
int main() { vector<int> vec; list<int> lst; find(vec.begin(), vec.end(), 42); find(lst.begin(), lst.end(), 42); }
Why? C++’s Computational Model Building Abstraction Building abstractions with objects The C++ Standard Library Generic Programming We’re about done!
2012– Andrei Alexandrescu.
58 / 59
c
2012– Andrei Alexandrescu.
59 / 59