1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos...

24
1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine

Transcript of 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos...

Page 1: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

1

Concepts: Linguistic Support for Generic Programming in C++Douglas Gregor

Jeremy SiekGabriel Dos Reis

Jaakko JärviBjarne Stroustrup

Andrew Lumsdaine

Page 2: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

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 algorithmn Start with concrete algorithms:

int gcd(int a, int b) { ... }n Remove unnecessary requirements:

template<typename Integral> Integral gcd(Integral a, Integral b) { ... }

n Repeat: lift Integral to CommutativeRing

Page 3: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

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?

Page 4: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

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

Page 5: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

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.

Page 6: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

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<typename T> where LessThanComparable<T> const T& min(const T& x, const T& y) { return x < y? x : y; }

Page 7: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

7

Concept Definitions Concept definitions state requirements on type

parameters.

concept LessThanComparable<typename T>{ 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; }}

Page 8: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

8

Concept Parameterization Concepts can have any number of parameters:

concept EqualityComparable<typename T, typename U> { bool operator==(T, U); bool operator!=(T, U);}

Page 9: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

9

Iterator Concepts Iterators abstract the notion of a sequence of values.

concept InputIterator<typename Iter> {

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};

Page 10: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

10

Iterators & Associated Types value_type is the type that the iterator points to

concept InputIterator<typename Iter> { 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};

Page 11: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

11

Iterators & Nested Requirements difference_type measures sequence length

concept InputIterator<typename Iter> { typename value_type; typename difference_type; where SignedIntegral<difference_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};

Page 12: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

12

Using Associated Types Implementing the STL find with concepts:

template<typename Iter, typename T>where InputIterator<Iter> && EqualityComparable<InputIterator<Iter>::value_type, T>Iter find(Iter first, Iter last, const T& value) { while (first != last && !(*first == value)) ++first; return first;}

Page 13: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

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<int*> { typedef int value_type; typedef ptrdiff_t difference_type;}

Page 14: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

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<typename T>concept_map InputIterator<T*> { typedef T value_type; typedef ptrdiff_t difference_type;}

Page 15: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

15

Concept Refinement A bidirectional iterator can move backward:concept BidirectionalIterator<typename Iter> : InputIterator<Iter> { Iter& operator--(Iter&); Iter operator--(Iter&, int);} A random access iterator can jump around:concept RandomAccessIterator<typename Iter> : BidirectionalIterator<Iter> { Iter operator+(Iter, difference_type); // …}

Input Iterator

BidirectionalIterator

Random AccessIterator

Page 16: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

16

Concept-Based Overloading Advance an iterator x by n steps:template<InputIterator Iter>void advance(Iter& x, Iter::difference_type n){ while (n > 0) { ++x; - -n; } } // O(n)

template<RandomAccessIterator Iter>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)

Page 17: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

17

Concept Maps for Composition

leda::GRAPH<Server, Link> internet_graph;leda::edge_array<double> total_latency;boost::shortest_paths(internet_graph, start, total_latency);

Page 18: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

18

Concept Maps for Composition

template<typename V, typename E>concept_map Graph<leda::GRAPH<V, E> > { typedef leda::leda_node vertex_type; int num_vertices(const leda::GRAPH<V, E>& g) { return g.number_of_nodes(); } int out_degree(vertex_type v, const leda::GRAPH<V, E>&) { return outdeg(v); }};

Page 19: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

19

Concept Maps for Composition

leda::GRAPH<Server> internet_graph;leda::edge_array<double> eigenvector;double eigenvalue = ietl::compute_eigenvector(internet_graph, eigenvector, 0);

Page 20: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

20

Concept Maps for Composition

template<typename V, typename E>concept_map Matrix<leda::GRAPH<V, E> > { // ...};

Page 21: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

21

Concept Maps for Composition

template<typename G>where Graph<G>concept_map Matrix<G> { // ...};

Page 22: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

22

Concept Maps for Composition

Page 23: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

23

Related Work

G

Haskell Type Classes

ML Signatures

Java, C# Generics

Fortress Traits

Page 24: 1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.

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