Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 of 24 Concepts: Linguistic Support for Generic Programming in C++ Douglas GregorJaakko JärviJeremy Siek Inidiana UniversityTexas A&M UniversityRice University.

Similar presentations


Presentation on theme: "1 of 24 Concepts: Linguistic Support for Generic Programming in C++ Douglas GregorJaakko JärviJeremy Siek Inidiana UniversityTexas A&M UniversityRice University."— Presentation transcript:

1 1 of 24 Concepts: Linguistic Support for Generic Programming in C++ Douglas GregorJaakko JärviJeremy Siek Inidiana UniversityTexas A&M UniversityRice University Bjarne Stroustrup Gabriel Dos RiezAndrew Lumsdaine Texas A&M University Texas A&M University Inidiana University OOPSLA- 2006

2 2 of 24 1. C++ Templates Today “ Dynamic Typing ” :  Templates are prepared to do anything !  If it fails – it fails ! Consequences:  Very powerful tool  Very difficult to use Templates are smart macros !!! Bjarne Sroustrup

3 3 of 24 1.1 Horrors of STL documentation Requirements table for Copy Constructible return typeexpression T(t) T(u) T::~T() T*&t Const T*&u Type T is a model of Copy Constructible, t is a value of type T and u is a value of type const T.

4 4 of 24 2. Motivation Consider : Attempt to compile - produces 2 Kb of error messages! Provided errors point to the implementation of STL sort()! The only clue that this error was triggered by users code: Actual error: STL sort() requires Random Access Iterartos.OutputOutput list lst; sort(lst.begin(),lst.end()); sort_list.cpp:8: instantiated from here

5 5 of 24 3. Concepts to the Rescue Concept: a set of requirements (syntactic, semantic, performance-related) on a template parameter.  Discussed extensively in the literature Modeling: A type that implements the requirements of a concept is said to model the concept Concept Refinement: A refines B if every type that models A also models B. C++ Concepts: language extension due to B.S and friends…  Objectives  Precise specification of requirements  Better than obscure documentation  Streamline writing and using templates  Backwards compatibility.  Preserve template flexibility and efficiency.

6 6 of 24 4. Language Support for Concepts Demonstrating major concept features : /* Concept Defenition */ concept LessThanComparable { bool operator<(T x,T y); } /* use constraint */ template where LessThanComparable const T& min(const T& x, const T& y){ return x<y ? x:y; }

7 7 of 24 4.1.1 Same-type constraints The only built-in concept. Requires that 2 type expressions produce same type. Built-in to enable the type checking of constrained templates. template<MutableForwardIterator iter1, MutableForwardIterator iter2> where SameType<iter1::value_type, iter2::value_type> void iter_swap(Iter1 a,Iter2 b){ swap(*a,*b); }

8 8 of 24 4.1.2 Negative constraints Why? Disambiguation! template void sort(S&) {} template where LessThanComparable void sort(S& s){ quick_sort(s); } … SortedArray arr; sort(arr) /* ERROR! Ambiguity ! */ template void sort(S&) {} template where LessThanComparable && !SortedSequence void sort(S& s){ quick_sort(s); } … SortedArray arr; sort(arr); /* OK ! No ambiguity ! */

9 9 of 24 4.1.3 Constraint Propagation Large template libraries: templates freely call each other. If template T1, calls T2, then T1 must make all demands that T2 places on X. Without propagation sort_container() would fail to compile, because it doesn’t guarantee the T is CopyConstructible. template class list{... }; template void sort_containter(list & c){ c.sort(); }

10 10 of 24 4.2. Concept Definition Namespace-level entity concept InputIterator : CopyConstructable /* refinement clause */{ /* Associated types */ typename value_type = Iter::value_type; typename pointer = Iter::pointer; /* Nested where clause */ where Arrowable... /* Signature */ Iter& operator++(Iter&); }

11 11 of 24 4.2.1 Refinement “Is-a” relationship between concepts. Concepts = sets of requirements, Refinement = set inclusion  "Multiple Inheritance” ? OK!  "Repeated Inheritance"? OK! A concept may refine any number of other concepts concept BidirectionalIterator : ForwardIterator {... };

12 12 of 24 4.2.2 Associated Types Often types rely on related types  E.g., graph type relies on edge and vertex. This concept requires from model type to name its edge and vertex types. A concept may provide a default for an associated type. concept Graph { typename edge; typename vertex;... edge find_edge(vertex u,vertex v,const G& g); };

13 13 of 24 4.2.3 Nested Requirements Allow the use of other concepts to constrain the parameters and associated types. Used to express the requirement that container’s value_type must be the same type as the iterators’s value_type. concept Container { typename value_type; InputIterator iterator; where SameType<value_type, InputIterator ::value_type>; }

14 14 of 24 4.2.4 Function Signatures An operator signature can be satisfied by free function or a member function definition. concept EqualityComparable { bool operator==(const T& x,const T& y); bool operator!=(const T& x,const T& y) { return !(x==y);} } concept Container { bool X::empty() const; X::X(int n); };

15 15 of 24 4.3. Overloading and Specialization Template function names overloaded on their where clause. template /* 1 */ void advance(Iter& iter,Iter::difference_type n){ while (n--) ++iter;} } template /* 2 */ void advance(Iter& iter,Iter::difference_type n){ if (n>0) {while (n--) ++iter;} else {while (n++) ++iter; } } template /* 3 */ void advance(Iter& iter,Iter::difference_type n){ iter += n; } void advancement(istream_iterator ii, list ::interator lsti,vector ::iterator vi){ advance(ii,17); /* advance 1 invoked */ advance(lsti,17); /* advance 2 invoked */ advance(vi,17);} /* advance 3 invoked */

16 16 of 24 4.4. Concept Maps Concept_map defines a mapping that states how the type models the concept. We don’t need to redesign student_record type! class student_record{ public: string id; string name; bool id_equal(const student_record& ); bool name_equal(const student_record& ); } concept_map EqualityComparbale { bool operator==(const student_record& x, const student_record& y) { return a.id_equal(b); } }

17 17 of 24 4.4. Concept Maps (cont.) Concept map templates used to compose generic libraries template concept_map Matrix { typedef int value_type; int rows(const G& g){return num_vertices(g);} int cols(const G& g){return num_vertices(g);} double operator()(const G& g,int i,int j){ edge e=find_edge(ith_vertex(i,g),g); if(e) return 1; else return 0; } My_graph g = read_graph(); vector x = compute_ith_eigenvector(g,0);

18 18 of 24 4.4.1 Implicit and Explicit Concepts To make bool fit it : concept EqualityComparable { bool operator==(const T& x,const T& y); bool operator!=(const T& x,const T& y); {return !(x==y);} } concept_map EqualityComparable { bool operator==(const bool& x, const bool& y){ return (x==y); } To make char fit it : concept_map EqualityComparable { bool operator==(const char& x, const char& y){ return (x==y); } To make it fit automatically : auto concept EqualityComparable { bool operator==(const T& x,const T& y); bool operator!=(const T& x,const T& y); {return !(x==y);} }

19 19 of 24 5. ConceptGCC = GCC + Concepts Inclusion Model: template definition must be available where template is used.  No separate compilation ConceptGCC: retain inclusion model compilation for backward compatibility and efficiency preservation. Precludes separate compilation for C++ templates. The interaction between specialization and separate compilation is being explored.

20 20 of 24 5.1 Implementation Technique Concepts: "compiled" to ordinary templates. Concept maps: "compiled" to specialized templates. concept Addable { T operator+(const T&,const T&); }; concept_map Addable { big_int operator+(const big_int& a, const big_int& b) {return a.plus(b);} }; template class Addable; template<> class Addable { static big_int operator+(const big_int& a, const big_int& b) {return a.plus(b);} };

21 21 of 24 5.2 Implementation Technique Constrained template: “compiled” to explicit calls. template where Addable && Assignable value_type sum(Iter first,Iter last, Iter::value_type s){ for(;first!=last;++first) s=s+*first; return s; } s = s + *first Addable ::operator+(s, InputIterator ::operator*(first))

22 22 of 24 5.3 Type-checking Templates ConceptGCC generates archetype – placeholder type. Archetypes define only operations stated by the concept requirements. Dependent expressions transformed into non- dependent in the first stage. Entire template definition is type-checked in parse-time. This way a modular type-checking is enabled.

23 23 of 24 6. Final STL example return typeexpression T(t) T(u) T::~T() T*&t Const T*&u Type T is a model of Copy Constructible, t is a value of type T and u is a value of type const T. STL documentation Concept translation auto concept CopyConstructible { T::T(const T&); T::~T(); T* operator&(T&); const T* operator&(const T&); };

24 24 of 24 6.1. STL Evaluation Results Uncovered STL errors and ambiguities. Concept-enhanced STL is better from user’s and implementer’s point of view. Provided (nearly) modular type-checking. Shorter and clearer error messages. Provided near-perfect backward compatibility. ConceptGCC available at www.generic-programming.org/software/GCC.www.generic-programming.org/software/GCC

25 1 of 24 Back…


Download ppt "1 of 24 Concepts: Linguistic Support for Generic Programming in C++ Douglas GregorJaakko JärviJeremy Siek Inidiana UniversityTexas A&M UniversityRice University."

Similar presentations


Ads by Google