Concepts in C - PowerPoint PPT Presentation

About This Presentation
Title:

Concepts in C

Description:

Concepts may define requirements on more than one types: ... Associated Types. Associated types: concept InputIterator typename Iter { typename value type; ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 35
Provided by: asztIn
Category:
Tags: concepts | define | lupin

less

Transcript and Presenter's Notes

Title: Concepts in C


1
Concepts in C
2
Templates in current C
  • C template is typeless
  • No language support for constrained generics
  • Accidental errors found in instantiation gt
  • complex, long, hard to understand error
    messages

3
Example
  • ...
  • stdlistltintgt intList
  • intList.push_back(42) ... intList.push_back(5)
  • ...
  • stdsort(intList.begin(), intList.end())
  • Compiled with gcc

4
Error messages
  • /usr/include/c/4.1.3/bits/stl_algo.h In
    function void stdsort(_RandomAccessIterator,
    _RandomAccessIterator) with _RandomAccessIterator
    std_List_iteratorltintgt
  • templ.cpp13 instantiated from here
  • /usr/include/c/4.1.3/bits/stl_algo.h2713
    error no match for operator- in __last -
    __first
  • /usr/include/c/4.1.3/bits/stl_algo.h In
    function void std__final_insertion_sort(_Random
    AccessIterator, _RandomAccessIterator) with
    _RandomAccessIterator std_List_iteratorltintgt
  • /usr/include/c/4.1.3/bits/stl_algo.h2714
    instantiated from void stdsort(_RandomAccessIte
    rator, _RandomAccessIterator) with
    _RandomAccessIterator std_List_iteratorltintgt
  • templ.cpp13 instantiated from here

5
Cont.
  • /usr/include/c/4.1.3/bits/stl_algo.h2360
    error no match for operator in __first 16
  • /usr/include/c/4.1.3/bits/stl_algo.h In
    function void std__insertion_sort(_RandomAccess
    Iterator, _RandomAccessIterator) with
    _RandomAccessIterator std_List_iteratorltintgt
  • /usr/include/c/4.1.3/bits/stl_algo.h2363
    instantiated from void std__final_insertion_sor
    t(_RandomAccessIterator, _RandomAccessIterator)
    with _RandomAccessIterator std_List_iteratorlt
    intgt
  • /usr/include/c/4.1.3/bits/stl_algo.h2714
    instantiated from void stdsort(_RandomAccessIte
    rator, _RandomAccessIterator) with
    _RandomAccessIterator std_List_iteratorltintgt
  • templ.cpp13 instantiated from here
  • /usr/include/c/4.1.3/bits/stl_algo.h2273
    error no match for operator in __first 1

6
Cont 2.
  • /usr/include/c/4.1.3/bits/stl_algo.h2363
    instantiated from void std__final_insertion_sor
    t(_RandomAccessIterator, _RandomAccessIterator)
    with _RandomAccessIterator std_List_iteratorlt
    intgt
  • /usr/include/c/4.1.3/bits/stl_algo.h2714
    instantiated from void stdsort(_RandomAccessIte
    rator, _RandomAccessIterator) with
    _RandomAccessIterator std_List_iteratorltintgt
  • templ.cpp13 instantiated from here
  • /usr/include/c/4.1.3/bits/stl_algo.h2279
    error no match for operator in __i 1

7
And the reason is
  • in the STL implementation?
  • The fact sort requires random access iterator
    but list has only forward iterator
  • But only this line refers to thattempl.cpp13
    instantiated from here
  • /usr/include/c/4.1.3/bits/stl_algo.h2273
    error no match for operator in __first 1

8
C0x
  • Well-known problem in industrial application
  • In other languages (java, Eiffel, ADA) there are
    constrained generics
  • In C0x, there will be concepts

9
The language components
  • Concept
  • Names requirements against template arguments
  • Concept_map
  • Semantic relationship between a type and a
    concept
  • Requires
  • The concrete requirement list agains template
    arguments of a class or function

10
Concept
  • List of function signatures and associated types
  • Like LessThanComparable concept
  • concept LessThanComparablelttypename Tgt
  • bool operatorlt(T a, T b)

11
Requires
  • LessThanComparable concept in a concrete template
    algorithm
  • templatelttypename Tgt
  • requires LessThanComparableltTgt
  • T min(const T a, const T b)?
  • return a lt b ? a b

12
Requires in other way
  • If a concept defines constraints on only one
    template parameter, there is a simpler way
  • templateltLessThanComparable Tgt
  • T min(const T a, const T b)?
  • return a lt b ? a b
  • Typeof a template

13
A bit more complex example
  • concept Regularlttypename Tgt
  • // default constructor
  • T T()
  • // copy constructor
  • T T(const T)
  • // destructor
  • TT()
  • // copy assignment
  • T operator(T, const T)
  • // equality comparison
  • bool operator(const T, const T)
  • // inequality comparison
  • bool operator!(const T, const T)
  • // swap
  • void swap(T, T)

14
Multi-Type Concept
  • Concepts may define requirements on more than one
    types
  • concept Convertiblelttypename T, typename Ugt
  • operator U(const T)
  • And there is a way to express default parameters
  • concept EqualtyComparablelttypename T, typename
    U Tgt
  • bool operator(T, U)?

15
Example of Multi-Type Concepts
  • concept InputIterator lttypename Iter, typename
    Valuegt
  • Iter Iter ()
    // default constructor
  • Iter Iter (const Iter )
    // copy constructor
  • Iter Iter ()
    // destructor
  • Iter operator(Iter, const Iter)
    // copy assignment
  • Value operator(const Iter)
    // dereference
  • Iter operator(Iter)
    // pre- increment
  • Iter operator(Iter, int)
    // post- increment
  • bool operator(const Iter, const Iter) //
    equality comparison
  • bool operator!(const Iter, const Iter ) //
    inequality comparison
  • void swap(Iter , Iter )
    // swap

16
Concepts composition
  • Regular concept used for InputIterator concept
  • concept InputIteratorlttypename Iter, typename
    Valuegt
  • requires RegularltItergt
  • Value operator(const Iter) // dereference
  • Iter operator(Iter) // pre-increment
  • Iter operator(Iter, int) // post-increment

17
Concepts refinement
  • concept InputIteratorlttypename Iter,typename
    Valuegt RegularltValuegt
  • Value operator(const Iter) // dereference
  • Iter operator(Iter) //
    pre-increment
  • Iter operator(Iter, int) //
    post-increment
  • Hint if hierarchy, then refinement,
  • Difference in concept_map definition

18
A template using InputIterator concept
  • templatelttypename Iter, Regular Vgt
  • requires InputIteratorltIter, Vgt
  • void print(Iter ?rst, Iter last)?
  • while (?rst ! last)?
  • stdcout ltlt first
  • stdcout ltlt stdendl

19
The real InputIterator concept
  • concept InputIterator
  • lttypename Iter, typename Value, typename
    Reference
  • typename Pointer,
    typename Di?erencegt
  • requires RegularltItergt
  • requires ConvertibleltReference, Valuegt
  • Reference operator(const Iter) //
    dereference
  • Iter operator(Iter) //
    pre-increment
  • Iter operator(Iter, int) //
    post-increment
  • // ...

20
The real template function using InputIterator
  • templatelttypename Iter, Regular V, typename R,
  • typename P, typename Dgt
  • requires InputIteratorltIter, V, R, P, Dgt
  • void print(Iter ?rst, Iter last)?
  • while (?rst ! last)?
  • stdcout ltlt first
  • stdcout ltlt stdendl

21
Associated Types
  • Associated types
  • concept InputIteratorlttypename Itergt
  • typename value type
  • typename reference
  • typename pointer
  • typename di?erence type
  • requires RegularltItergt
  • requires Convertibleltreference type, value
    typegt
  • reference operator(const Iter)
  • Iter operator(Iter)
  • Iter operator(Iter, int)
  • // ...

22
Template using InputIterator
  • Now we have only one template parameter
  • templateltInputIterator Itergt
  • requires RegularltItervalue typegt
  • void print(Iter ?rst, Iter last)
  • while (?rst ! last)
  • stdcout ltlt first
  • stdcout ltlt stdendl

23
Concept_map
  • Semantic relationship between a type and a
    concept
  • Example looking for the best student
  • struct Student
  • stdstring name
  • double avg
  • ...
  • stdlistltStudentgt l
  • ...
  • stdcout ltlt (stdmin_element(l.begin(),
    l.end())).name ltlt stdendl

24
Concept_map 2
  • But operatorlt is not defined on Student
  • Non-intrusive solution using concept_map
  • concept_map LessThanComparableltDiakgt
  • bool operatorlt(Diak a, Diak b)
  • return a.atlag lt b.atlag

25
Concept_map 3
  • If operatorlt is defined on some type X, we dont
    need to redefine
  • concept_map LessThanComparableltXgt
  • auto keyword no concept_map is required

26
Other examples for Concept_map
  • Suppose InputIterator is auto
  • Then this partial concept_map definition is valid
  • concept map InputIteratorltchargt
  • typedef char value type
  • typedef char reference
  • typedef char pointer
  • typedef stdptrdi?_t di?erence type
  • The rest is generated automaticaly

27
More Concept_map
  • If implicite (auto) declaration is not valid,
    e.c. we want to define to use int-s, az
    iterator
  • concept map InputIteratorltintgt
  • typedef int value type
  • typedef int reference
  • typedef int pointer
  • typedef int difference type
  • int operator(int x) return x
  • Thus, we could inialitize containers.
  • stdcopy(1, 101, v.begin())

28
Concept_map template
  • concepts and concept_maps can be tempatized
  • All the pointers are iterators
  • templatelttypename Tgt
  • concept_map InputIteratorltTgt
  • typedef T value type
  • typedef T reference
  • typedef T pointer
  • typedef std ptrdi? t di?erence type

29
A container adaptor
  • With concept, concept_map container adaptors
    could be defined without wrapper classes.
  • concept Stacklttypename Xgt
  • typename value type
  • void push(X, const value type)
  • void pop(X)
  • value type top(const X)
  • bool empty(const X)

30
And its concept_map
  • templatelttypename Tgt
  • concept_map StackltstdvectorltTgt gt
  • typedef T value type
  • void push(stdvectorltTgt v, const T x)
  • v.push_back(x)
  • void pop(std vector ltTgt v)
  • v.pop_back()
  • T top(const stdvector ltTgt v)
  • return v.back()
  • bool empty(const stdvector ltTgt v)
  • return v.empty()

31
Concept-based Overloading
  • Best matching template will be instantiated,
  • Better way to implement many STl functions.

32
The advance fv. declarations
  • templateltInputIterator Itergt
  • void advance(Iter x, Iterdifference_type n)
  • while (n gt 0) x --n
  • templateltBidirectionalIterator Itergt
  • void advance(Iter x, Iterdifference_type n)
  • if (n gt 0) while (n gt 0) x --n
  • else while (n lt 0) --x n
  • templateltRandomAccessIterator Itergt
  • void advance(Iter x, Iterdifference_type n)
  • x n

33
Negative constraint
  • templateltEqualityComparable Tgt
  • class dictionary
  • // slow, linked-list implementation
  • templateltLessThanComparable Tgt
  • requires !HashableltTgt
  • class dictionaryltTgt
  • // balanced binary tree implementation
  • templateltHashable Tgt
  • class dictionaryltTgt
  • // hash table implementation

34
The error messages
  • For the first example
  • ...
  • stdlistltintgt intList
  • intList.push_back(42) ... intList.push_back(5)
  • ...
  • stdsort(intList.begin(), intList.end())
  • We got this compiler error
  • listconc.cpp In function int main()
  • listconc.cpp9 error no matching function for
    call to sort(std_List_iteratorltintgt,
    std_List_iteratorltintgt)
  • /home/lupin/local/conceptgcc/bin/../lib/gcc/i686-p
    c-linux-gnu/4.3.0/../../../../include/c/4.3.0/bi
    ts/stl_algo.h2874 note candidates are void
    stdsort(_Iter, _Iter) with _Iter
    std_List_iteratorltintgt ltwhere clausegt
  • listconc.cpp9 note no concept map for
    requirement stdMutableRandomAccessIteratorltstd
    _List_iteratorltintgt gt
Write a Comment
User Comments (0)
About PowerShow.com