concepts lite

Download Concepts lite

Post on 15-Apr-2017

1.339 views

Category:

Software

0 download

Embed Size (px)

TRANSCRIPT

  • Concepts Concepts Lite

  • Concepts Lite

  • 4

  • 5

    1>include\algorithm(3157): error C2784: 'unknown-type std::operator -(std::move_iterator &,const std::move_iterator &)' : 1> could not deduce template argument for 'std::move_iterator &' from 1> 'std::_List_iterator' 1> include\xutility(1969) : see declaration of 'std::operator -' 1> include\algorithm(3164) : see reference to function template instantiation 1> 'void std::sort(_RanIt,_RanIt,_Pr)' being compiled 1> with 1> [ 1> _RanIt=std::_List_iterator 1> , _Pr=std::less 1> ] 1> main.cpp(8) : see reference to function template instantiation 1> 'void std::sort(_RanIt,_RanIt)' being compiled 1> with 1> [ 1> _RanIt=std::_List_iterator 1> ] 1> include\xutility(1969) : see declaration of 'std::operator -' 1> include\xutility(1969) : see declaration of 'std::operator -' 1> include\xutility(1969) : see declaration of 'std::operator -' 1> include\xutility(1969) : see declaration of 'std::operator -' 1>include\algorithm(3157): error C2784: 'unknown-type std::operator -(const std::reverse_iterator &,const std::reverse_iterator &)' : 1> could not deduce template argument for 'const std::reverse_iterator &' 1> from 'std::_List_iterator' 1> include\xutility(1130) : see declaration of 'std::operator -' 1> include\xutility(1130) : see declaration of 'std::operator -' 1> include\xutility(1130) : see declaration of 'std::operator -' 1> include\xutility(1130) : see declaration of 'std::operator -' 1> include\xutility(1130) : see declaration of 'std::operator -' 1>include\algorithm(3157): error C2784: 'unknown-type std::operator -(const std::_Revranit &,const std::_Revranit &)' : 1> could not deduce template argument for 'const std::_Revranit &' 1> from 'std::_List_iterator' 1> include\xutility(937) : see declaration of 'std::operator -' 1> include\xutility(937) : see declaration of 'std::operator -' 1> include\xutility(937) : see declaration of 'std::operator -' 1> include\xutility(937) : see declaration of 'std::operator -' 1> xutility(937) : see declaration of 'std::operator -' 1>include\algorithm(3157): error C2676: binary '-' : 'std::_List_iterator' does not define this 1> operator or a conversion to a type acceptable to the predefined operator 1>include\algorithm(3157): error C2780: 'void std::_Sort(_RanIt,_RanIt,_Diff,_Pr)' : expects 4 arguments - 3 provided 1> include\algorithm(3121) : see declaration of 'std::_Sort'

    std::list list; std::sort(list.begin(), list.end());

  • C

    6

    main.cpp(8): list::iterator is not a RandomAccessIterator

    O_O

  • 7

    template typename std::enable_if< is_input_iterator && !is_bidirectional_iterator >::type advance(Iterator &i, Difference n) { while(n--) ++i; } template typename std::enable_if< is_bidirectional_iterator && !is_random_access_iterator >::type advance(Iterator &i, Difference n) { if (n > 0) while (n--) ++i; if (n < 0) while (n++) --i; } template typename std::enable_if::type advance(Iterator &i, Difference n) { i += n; }

  • 8

    void advance(InputIterator &i, Difference n) { while(n--) ++i; }

    template advance(Iterator &i, Difference n) { while(n--) ++i; }

    O_O

  • 9

    :

    :

    interface List extends Collection {...} static void sort(List list);

    template concept bool Sortable() {...} template void sort(Container& c);

    Java C++

  • 10

  • ++

    11

    1995 2000 2005 2010 2015 2020

    1994: STL ++.

    Pre-Standard ++98 ++03 ++11 ++14 ++17+

    1996: STL .

    2002: boost . . .

    2005: C++0x.

    2009: C++0x.

    2013: Concepts Lite .

    2017: Concepts Lite C++17

  • 12

    ++0x?

  • 13 13

    template requires Assignable void fill(I first, I last, const V& v); fill(0, 9, 9.9); // Error: int is not a ForwardIterator fill(&v[0], &v[9], 9.9); // OK

  • 14

    template requires Assignable void fill(I first, I last, const V &v) { while (first != last) { *first = v; first = first + 1; // Error: operator+ not defined // for ForwardIterator } }

  • 15

    concept Storable { void store_value(T); }; template void store(const T& value) { store_value(value); // OK }

  • 16

    concept Storable { void store_value(T); }; template void store(const T& value) { store_value(value); // OK #ifdef _DEBUG std::cerr

  • 17

    concept Storable { void store_value(T); }; template void store(const T& value) { store_value(value); // OK #ifdef _DEBUG std::cerr

  • concept_map

    18

    concept RandomAccessIterator { /* ... */ }; template typename I::value_type range_max(I first, I last); int array[N] = { /* ... */ }; std::cout

  • concept_map

    19

    concept RandomAccessIterator { /* ... */ }; template concept_map RandomAccessIterator { typedef T value_type; }; template typename I::value_type range_max(I first, I last); int array[N] = { /* ... */ }; std::cout

  • 20

    concept TotalOrdering { bool operator()(Op, T, T); axiom Antisymmetry(Op op, T x, T y) { if (op(x, y) && op(y, x)) x y; } axiom Transitivity(Op op, T x, T y, T z) { if (op(x, y) && op(y, z)) op(x, z); } axiom Totality(Op op, T x, T y) { op(x, y) || op(y, x); } }

  • 21

  • Concepts Lite

    22

  • Concepts Lite

    23

    concept_map

  • 25

    template void sort(C& container);

    error: no matching function call to sort(list&) note: candidate is sort(C& container) note: where C = list note: template constraints not satisfied note: C is not a/an Sortable type

    list l; sort(l);

  • 26

    template concept bool Sortable = Permutableontainer && TotallyOrdered; template concept bool Integral() { return std::is_integral(); }

    constexpr !

  • 27

    template class vector; /* Equivalently: */ template requires Object && Allocator class vector;

  • 28

    template Iterator_type find(S&& sequence, const T& value); /* Equivalently: */ template requires Sequence && Equality_comparable Iterator_type find(S&& sequence, const T& value); /* Or: */ template requires Equality_comparable Iterator_type find(S&& sequence, const T& value);

  • 29

    template class vector { public: requires Copyable vector(const vector& x); requires Movable void push_back(T&& x); };

  • 30

    template void advance(I& i, DifferenceType n) { while (n--) ++i; } template void advance(I& i, DifferenceType n) { if (n > 0) while (n--) ++i; if (n < 0) while (n++) --i; } template void advance(I& i, DifferenceType n) { i += n; }

  • 31

    istream_iterator iter(cin); advance(iter, 1); // Input overload vector::iterator first = v.begin(); advance(first, 1); // Random access overload

    .

  • 32

    template struct numeric_traits; template struct numeric_traits : integral_traits { /*...*/ }; template struct numeric_traits : floating_point_traits { /*...*/ };

  • -

    33

    vector v = { /*...*/ }; find_if(v, [](const auto& x) { return x == "hello"; });

    template concept bool String = /*...*/;

    [](const String& x) { return x == "hello"; } [](const T& x) { return x == "hello"; } [] String{T} (const T& x) { return x == "hello"; }

  • -

    34

    template concept bool Input_query() = Input_iterator && Predicate;

    template requires Input_query I find_if(I first, I last, P pred); /* As a lambda: */ auto find_if = [] Input_query{I, P} (I first, I last, P pred) {}; /* As a function with {} syntax: */ Input_query{I, P} I find_if(I first, I last, P pred);

  • auto

    35

    Integer gcd(Integer a, Integer b) { /* ... */ } /* Equivalently: */ template T gcd(T a, T b);

    Real y = f(x); /* Equivalently: */ auto y = f(x); static_assert(Real(), "");

  • requires

    36

    template concept bool Equality_comparable = requires (T a, T b) { {a == b} -> bool; {a != b} -> bool; }; template concept bool Readable = requires (I i) { typename Value_type; {*i} -> const Value_type&; };

  • 37

    bool

  • 38

    template requires Equality_comparable bool distinct(T a, T b) { return a != b; } /* Decomposed: */ template requires __is