Mircea Baja @ ACCU Oxford - Lightning Talks - 18th July 2017 # C++ concepts basics C++ concepts (N4647): cover the basics and show how they relate to templates (the "peel off onion layers" technique). (v2) --- # Simple generic function ```cpp template
const T & min(const T & a, const T & b) { // implementation here } ``` -- ```cpp if (a < b) return a; else return b; // 1 if (a <= b) return a; else return b; // 2 if (a > b) return b; else return a; // 3 if (b < a) return b; else return a; // 4 ``` --- # Concept definition ```cpp template
concept bool StrictlyTotallyOrdered() { return EqualityComparable
() && requires (const T a, const T b) { { a < b } -> Boolean; { a > b } -> Boolean; { a <= b } -> Boolean; { a >= b } -> Boolean; }; } ``` -- requires above is a requires-expression --- # Usage ```cpp const StrictlyTotallyOrdered & min( const StrictTotallyOrdered & a, const StrictTotallyOrdered & b) { if (b < a) return b; else return a; } ``` -- ```cpp template
const T & min(const T & a, const T & b) { if (b < a) return b; else return a; } ``` -- ```cpp template
requires StrictlyTotallyOrdered
() const T & min(const T & a, const T & b) { if (b < a) return b; else return a; } ``` requires above is a requires-clause --- # Constraints ```cpp requires(const B b1, const B b2) { bool(b1); // expression constraint typename B::type; // type constraint { !b1 } -> bool; // implicit conversion { b1 && b2 } -> Same
; // argument deduction } ``` -- ```cpp void fn(Same
x); // invent fn(b1 && b2); // check if it compiles ``` -- ```cpp template
T> void fn(T x); ``` -- ```cpp template
requires Same
void fn(T x); ``` --- # References Concepts overview (Bjarne Stroustrup) http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0557r0.pdf Concepts proposal wording (Andrew Sutton) http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4674.pdf Ranges TS (Eric Niebler, Casey Carter) http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4671.pdf Elements of Programming (Alexander A. Stepanov and Paul McJones) --- # Questions? https://bajamircea.github.io/presentations/2017-07-18-concepts-basics.html