1 / 94

Overview

14. Overview. Miscellaneous Utility Functions Return Type Deduction Generic Lambdas Generalised Lambda Captures C++14 Literals constexpr Functions Variable Templates Shared Mutexes and Locking Improved <functional > O perators Tuple Addressing via Type

moira
Download Presentation

Overview

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 14

  2. Overview • MiscellaneousUtilityFunctions • Return Type Deduction • GenericLambdas • GeneralisedLambdaCaptures • C++14 Literals • constexprFunctions • Variable Templates • Shared MutexesandLocking • Improved<functional>Operators • TupleAddressing via Type • Type Deductionanddecltype(auto)

  3. MiscellaneousUtilityFunctions std::make_unique std::cbegin, std::cend, … std::exchange Robuust Sequence Operations quotedStream Manipulator

  4. std::make_unique<memory> usingnamespacestd; auto answer = make_unique<int>(42);auto zero = make_unique<int>(); auto array = make_unique<int[]>(10); auto v = make_unique<vector<int>>(5,-1); structPerson { Person(string,string, int) {}; } auto me = make_unique<Person> ("Peter","Van Weert", 1983); std::unique_ptr<int>

  5. std::make_unique<memory> • make_unique<LongTypeName>(…)versus unique_ptr<LongTypeName>( new LongTypeName(…));

  6. std::make_unique<memory> • make_unique<LongTypeName[]>(…)versus unique_ptr<LongTypeName[]>( newLongTypeName[…]); • f(unique_ptr<X>(new X(…)), unique_ptr<Y>(new Y(…)), g(…));versusf(make_unique<X>(…),make_unique<Y>(…), g(…));

  7. MiscellaneousUtilityFunctions • std::cbegin+std::cend<iterator> • E.g. cbegin(myVector) i/o myVector.cbegin() • Works on C-style arrays • Convenientfortemplatised code • Alsostd::rbegin/rend, std::crbegin/crend • std::exchange <utility> auto oldVal = std::exchange(x, newVal);

  8. MiscellaneousUtilityFunctions • Robuust SequenceOperations <algorithm> • equal(v1.begin(),v1.end(), v2.begin())  equal(v1.begin(),v1.end(),v2.begin(),v2.end()) • Analogousformismatch and is_permutation • quotedStreamManipulator<iomanip> • cout<< std::quoted("Hello World!"); > "HelloWorld!" • cin << std::quoted(myString, '\''); < 'Hello Computer!!' assert(myString == "Hello Computer!!");

  9. OtherMiscellaneous Utilities • Null forward iterators • Discourageduse of rand() • [[deprecated]]attribute • …

  10. LambdaExpressions (C++11) LambdaExpressions as AnonymousFunctions

  11. LambdaExpressions (C++11) std::vector<int> v{ 7, 9, 7, 2, 0, 4 }; std::sort(v.begin(), v.end(), [](inta, intb) { returna > b; } );

  12. FunctionObjects (C++98) std::vector<int> v{ 7, 9, 7, 2, 0, 4 }; structLambda { bool operator()(inta, intb) const { returna > b; } }; std::sort(v.begin(), v.end(), Lambda());

  13. LambdaExpressions (C++11) autocomp = [](inta, intb) { returna > b; }; std::vector<int> v{ 7, 9, 7, 2, 0, 4 };std::sort(v.begin(), v.end(), comp); bool(*fptr)(int,int) = comp;std::function<bool(int,int)>fun = comp; std::cout << comp(123, 45) << std::endl;

  14. Return Type Deduction Lambda Return Type Deduction (C++11) Lambda Return Type Deduction (C++14) NormalFunction Return Type Deduction (C++14)

  15. Lambda Return Type Deduction (C++11) usingnamespacestd; vector<int> v{ 7, 9, 7, 2, 0, 4 }; auto newEnd = remove_if(begin(v),end(v), [](inta) { returna % 2 == 1; } ); v.erase(newEnd, end(v));std::for_each(cbegin(v), cend(v), [](inta) { cout << a << endl; });

  16. Lambda Return Type Deduction (C++11) usingnamespacestd; vector<int> v{ 7, 9, 7, 2, 0, 4 }; autonewEnd = remove_if(begin(v), end(v), [](inta) {if (a % 2 == 0) returnfalse; cout<< "Removed " << a << endl; returntrue;}); v.erase(newEnd, end(v));

  17. Lambda Return Type Deduction (C++11) usingnamespacestd; vector<int> v{ 7, 9, 7, 2, 0, 4 }; autonewEnd = remove_if(begin(v), end(v), [](inta) -> bool {if (a % 2 == 0) returnfalse; cout<< "Removed " << a << endl; returntrue;}); v.erase(newEnd, end(v));

  18. Lambda Return Type Deduction (C++14) usingnamespacestd; vector<int> v{ 7, 9, 7, 2, 0, 4 }; autonewEnd = remove_if(begin(v), end(v), [](inta) {if (a % 2 == 0) returnfalse; cout<< "Removed " << a << endl; returntrue;}); v.erase(newEnd, end(v));

  19. LambdaReturn Type Deduction (C++14) usingnamespacestd; string k3[] {"Karen","Kristel","Kathleen"}; transform(cbegin(k3), cend(k3), begin(k3), [](conststring& s) { if (s.empty()) return" ";elsereturns.substr(0, 1);});

  20. Lambda Return Type Deduction (C++14) usingnamespacestd; string k3[] {"Karen","Kristel","Kathleen"}; transform(cbegin(k3), cend(k3), begin(k3), [](conststring& s) { return s.empty()? " ":s.substr(0, 1);});

  21. LambdaReturn Type Deduction (C++14) usingnamespacestd; string k3[] {"Karen","Kristel","Kathleen"}; transform(cbegin(k3), cend(k3), begin(k3), [](conststring& s) -> string { if (s.empty()) return" ";elsereturns.substr(0, 1);});

  22. Lambda Return Type Deduction (C++14) usingnamespacestd; string k3[] {"Karen","Kristel","Kathleen"}; transform(cbegin(k3), cend(k3), begin(k3), [](conststring& s) { if (s.empty()) returnstring(" ");elsereturns.substr(0, 1);});

  23. Lambda Return Type Deduction (C++14) usingnamespacestd; string k3[] {"Karen","Kristel","Kathleen"}; transform(cbegin(k3), cend(k3), begin(k3), [](conststring& s) { if(!s.size()) return" ";elsereturns.substr(0, 1).c_str();});

  24. Function Return Type Deduction (C++14) std::vector<std::string>::const_iterator autofind(conststd::vector<std::string>& v){for (auto i = cbegin(v); i!= cend(v); ++i) {if(*i== "Waldo")returni; }returncend(v);}

  25. Function Return Type Deduction (C++14) unsignedlonglong auto fib(unsignedi){if (i < 2)return 1ull;elsereturn fib(i - 1) + fib(i - 2);}

  26. Function Return Type Deduction (C++14) auto fib(unsignedi){if(i>= 2)returnfib(i - 1) + fib(i- 2); elsereturn1ull;}

  27. Function Return Type Deduction (C++14) auto fib(unsignedi){if(i>= 2)returnfib(i - 1) + fib(i- 2); elsereturn1ull;}

  28. Return Type Deduction • C++11 • Lambda return type deduction (implicit ‘-> auto’) • Single return statement or void • C++14 • Multiple statements • Multiple return statements of same type • Normal function return type deduction (auto) • Recursion once return type is deduced • Functionswith auto return type cannotbe virtual

  29. GenericLambdas

  30. GenericLambdas static auto plus = [](auto x, auto y) { returnx + y; }; std::vector<int> x{ 1, 2, 3 }; std::vector<int> y{ 4, 5, 6 }; std::vector<int> z(3); std::transform(x.begin(), x.end(),y.begin(), z.begin(), plus);

  31. GenericLambdas static auto plus = [](auto x, auto y) { returnx + y; }; std::vector<double> x{ 1.0, 2.0, 3.0 }; std::vector<double> y{ 4.0, 5.0, 6.0 }; std::vector<double> z(3); std::transform(x.begin(), x.end(),y.begin(), z.begin(), plus);

  32. GenericLambdas static auto plus = [](auto x, auto y) { returnx + y; }; std::vector<double> x{ 1.0, 2.0, 3.0 }; std::vector<int> y{ 4, 5, 6 }; std::vector<double> z(3); std::transform(x.begin(), x.end(),y.begin(), z.begin(), plus);

  33. GenericLambdas static auto plus = [](auto x, auto y) { returnx + y; }; vector<string> x{"one", "two", "three"}; vector<string> y{"four", "five", "six"}; vector<string> z(3); std::transform(x.begin(), x.end(),y.begin(), z.begin(), plus);

  34. GenericLambdas static auto plus = [](auto&&x,auto&&y) { returnx + y; }; vector<string> x{"one", "two", "three"}; vector<string> y{"four", "five", "six"}; vector<string> z(3); std::transform(x.begin(), x.end(),y.begin(), z.begin(), plus);

  35. GenericLambdas static auto plus = [](auto x, auto y) { returnx + y; }; int (*fptr1)(int, int) = plus; double (*fptr2)(int, double) = plus; function<float(float,float)> f(plus); std::cout << plus(123,456) << std::endl;

  36. GenericLambdas static auto plus = [](auto&&x,auto&&y) { returnx + y; }; structPlus{ template<typenameT1, typenameT2>auto operator(T1&& x, T2&& y)const { returnx + y; };/* Function pointer conversion... */};

  37. GeneralisedLambdaCaptures LambdaCaptures (C++11) GeneralisedLambdaCaptures (C++14)

  38. LambdaCaptures (C++11) usingnamespacestd; vector<float> v{ 7, 9, 7, 2, 0, 4 }; floatavg = Average(v); double std = ...

  39. LambdaCaptures (C++11) usingnamespacestd; vector<float> v{ 7, 9, 7, 2, 0, 4 }; floatavg = Average(v);double std = sqrt(accumulate(cbegin(v), cend(v), 0., [avg](double acc, floatx){return acc+= (x-avg) * (x-avg);}) / v.size());

  40. GeneralisedLambdaCaptures (C++14) usingnamespacestd; vector<float> v{ 7, 9, 7, 2, 0, 4 }; double std = sqrt(accumulate(cbegin(v), cend(v), 0., [avg= Average(v)](double a, floatx){return a += (x-avg) * (x-avg);}) / v.size());

  41. LambdaCaptures (C++11) constunsignednum = 100;std::vector<unsigned> v(num); unsignedcounter = 0;std::generate_n(v.begin(), num, [&counter]() { return ++counter; } );

  42. LambdaCaptures (C++11) constunsignednum = 100;std::vector<unsigned> v(num); unsignedcounter = 0;std::generate_n(v.begin(), num, [counter]() { return ++counter; } );

  43. LambdaCaptures (C++11) constunsignednum = 100;std::vector<unsigned> v(num); unsignedcounter = 0;std::generate_n(v.begin(), num, [counter]() mutable { return ++counter; } );

  44. GeneralisedLambdaCaptures (C++14) constunsignednum = 100;std::vector<unsigned> v(num); std::generate_n(v.begin(), num, [counter = 0u]() mutable { return ++counter; } );

  45. GeneralisedLambdaCaptures (C++14) constunsignednum = 100;std::vector<unsigned> v(num); std::generate_n(v.begin(), num, [/*auto */counter = 0u]() mutable { return ++counter; } );

  46. LambdaCaptures (C++11) usingFilter = std::function<bool(conststd::string&>; virtual Filter GetFilter(/*...*/) override{usingnamespacestd; unordered_set<string> huge;/* Gather strings in huge... */ return [=](conststring& s) { return huge.count(s) > 0; }}

  47. LambdaCaptures (C++11) usingFilter = std::function<bool(conststd::string&>; virtual Filter GetFilter(/*...*/) override{usingnamespacestd; auto_ptr<unordered_set<string>>huge(newunordered_set<string>);/* Gather strings in huge... */ return [=](conststring& s) { return huge->count(s) > 0; }}

  48. GeneralisedLambdaCaptures (C++14) usingFilter = std::function<bool(conststd::string&>; virtual Filter GetFilter(/*...*/) override{usingnamespacestd; unordered_set<string> huge;/* Gather strings in huge... */ return [huge=move(huge)](conststring& s) { return huge.count(s) > 0; }}

More Related