Download presentation
Presentation is loading. Please wait.
Published bySamuel Chase Modified over 9 years ago
1
1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine
2
2 Generic Programming A methodology for the construction of generic software libraries. Dual focus on abstraction and efficiency Example: The C++ Standard Template Library Core notion: Lifting an algorithm 1. Start with concrete algorithms: int gcd(int a, int b) {... } 2. Remove unnecessary requirements: template Integral gcd(Integral a, Integral b) {... } 3. Repeat: lift Integral to CommutativeRing
3
3 Generic Programming in C++ C++ templates enable the application of GP Overloading permits natural abstractions Instantiation eliminates cost of abstractions Many successful, generic libraries in C++ Significant problems remain: Inability to directly express ideas of GP Generic libraries in C++ are fragile Can we design better support for Generic Programming in C++ without sacrificing the performance and flexibility of templates?
4
4 Concepts for C++: Goals Support for the core ideas of Generic Programming in C++ Modular type checking for C++ templates Performance equivalent to C++ templates Complete backward compatibility Library evolution is particularly important Simplicity Implementability
5
5 Concepts Overview Three major parts: Concept definitions: Specify the behavior of classes of types via requirements. Where clauses: Specify constraints on template parameters in terms of concepts. Concept maps: Specify how types meet the requirements of a concept.
6
6 Constrained Templates Place constraints on template parameters via a where clause Uses of the template must satisfy these constraints Definition of the template can assume only what the constraints imply template where LessThanComparable const T& min(const T& x, const T& y) { return x < y? x : y; }
7
7 Concept Definitions Concept definitions state requirements on type parameters. concept LessThanComparable { bool operator<(T, T); axiom Irreflexivity(T x) { !(x < x); } axiom Transitivity(T x, T y, T z) { if (x < y && y < z) x < z; }
8
8 Concept Parameterization Concepts can have any number of parameters: concept EqualityComparable<typename T, typename U> { bool operator==(T, U); bool operator!=(T, U); }
9
9 Iterator Concepts Iterators abstract the notion of a sequence of values. concept InputIterator { Iter& operator++(Iter&); // pre-increment Iter operator++(Iter&, int); // post-increment bool operator==(Iter, Iter); // equality comparison bool operator!=(Iter, Iter); // inequality comparison ??? operator*(Iter); // dereference };
10
10 Iterators & Associated Types value_type is the type that the iterator points to concept InputIterator { typename value_type; Iter& operator++(Iter&); // pre-increment Iter operator++(Iter&, int); // post-increment bool operator==(Iter, Iter); // equality comparison bool operator!=(Iter, Iter); // inequality comparison value_type operator*(Iter); // dereference };
11
11 Iterators & Nested Requirements difference_type measures sequence length concept InputIterator { typename value_type; typename difference_type; where SignedIntegral ; Iter& operator++(Iter&); // pre-increment Iter operator++(Iter&, int); // post-increment bool operator==(Iter, Iter); // equality comparison bool operator!=(Iter, Iter); // inequality comparison value_type operator*(Iter); // dereference };
12
12 Using Associated Types Implementing the STL find with concepts: template where InputIterator && EqualityComparable ::value_type, T> Iter find(Iter first, Iter last, const T& value) { while (first != last && !(*first == value)) ++first; return first; }
13
13 Concept Maps We want to call find with an array of integers: bool contains(int* array, int n, int value) { return find(array, array + n, value) != array + n; } Concept maps satisfy concept constraints: concept_map InputIterator { typedef int value_type; typedef ptrdiff_t difference_type; }
14
14 Concept Maps We want to call find with an array of integers: bool contains(int* array, int n, int value) { return find(array, array + n, value) != array + n; } Concept maps satisfy concept constraints: template concept_map InputIterator { typedef T value_type; typedef ptrdiff_t difference_type; }
15
15 Concept Refinement A bidirectional iterator can move backward: concept BidirectionalIterator : InputIterator { Iter& operator--(Iter&); Iter operator--(Iter&, int); } A random access iterator can jump around: concept RandomAccessIterator : BidirectionalIterator { Iter operator+(Iter, difference_type); // … } Input Iterator Bidirectional Iterator Random Access Iterator
16
16 Concept-Based Overloading Advance an iterator x by n steps: template void advance(Iter& x, Iter::difference_type n) { while (n > 0) { ++x; - -n; } } // O(n) template void advance(Iter& x, Iter::difference_type n) { x = x + n; } // O(1) Compiler selects best match: advance(i, n); // O(1) or O(n)? Overloaded calls in generic algorithms can cause instantiation-time ambiguities (PLDI ‘06)
17
17 Concept Maps for Composition leda::GRAPH internet_graph; leda::edge_array total_latency; boost::shortest_paths(internet_graph, start, total_latency);
18
18 Concept Maps for Composition template concept_map Graph > { typedef leda::leda_node vertex_type; int num_vertices(const leda::GRAPH & g) { return g.number_of_nodes(); } int out_degree(vertex_type v, const leda::GRAPH &) { return outdeg(v); } };
19
19 Concept Maps for Composition leda::GRAPH internet_graph; leda::edge_array eigenvector; double eigenvalue = ietl::compute_eigenvector(internet_graph, eigenvector, 0);
20
20 Concept Maps for Composition template concept_map Matrix > { //... };
21
21 Concept Maps for Composition template where Graph concept_map Matrix { //... };
22
22 Concept Maps for Composition
23
23 Related Work G Haskell Type Classes ML Signatures Java, C# Generics Fortress Traits
24
24 Summary: Concepts for C++ Concepts provide complete support for Generic Programming in C++ Transparent composition of generic libraries Seamless evolution of existing C++ code Prototype implementation in ConceptGCC Includes drop-in concept-enhanced STL http://www.generic-programming.org/software/ConceptGCC Strong candidate for inclusion in upcoming ISO C++ Standard, C++0x
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.