Back to Basics: Lambda Expressions## Back to Basics Lambda Expressions Barbara Geller & Ansel Sermersheim CppCon September 2020 ## I ntroduction • Prologue • History • Function Pointer • Function Object • Definition of a Lambda library ☐ CsLibGuarded ☑ library for managing access to data shared between threads ## Lambda Expressions ## • History ○ $ \lambda $ calculus is a branch of mathematics ■ introduced in the 1930’s memory ■ languages which were influenced by lambda calculus • Haskell, LISP, and ML ## Lambda Expressions ## • History ☐ why do we use the terminology lambda expression ☑ greek letter $ \lambda $0 码力 | 48 页 | 175.89 KB | 1 年前3
Six Ways for Implementing Math Expressions Calculator## +23 ## Six Ways for Implementing Math Expressions Calculator ## AMIR KIRSH 20 23 October 01 - 06 ## About me ## Lecturer Academic College of Tel-Aviv-Yaffo and Tel-Aviv University ## Developer [Image](/uploads/documents/4/5/b/8/45b8c80c4fc816ade243ff11567a25e4/p9_2.jpg) # Six ways for Implementing Math Expressions Calculator A walk through polymorphism, smart pointers, templates, concepts and more Amir Kirsh necessarily...) Shorter (and nicer?) code. No need for allocations! And... we may even evaluate expressions at compile time! ## Let's see the code http://coliru.stacked-crooked.com/a/5060e5f1891353620 码力 | 63 页 | 1.85 MB | 1 年前3
The Many Shades of reference_wrapperreference_wrapper closely matches lvalue references • it may refer to const objects • it does not bind to rvalue expressions • declaring reference_wrapperdoes not odr-use T ## Deduce from const-qualified lvalues value is an rvalue. • Unlike rvalues of other types, a pointer value can be treated as an lvalue (e.g., via unary operator *). - A pointee is a variable whose lvalue corresponds to the rvalue of some pointer • A pointer value is simultaneously: • An lvalue (from the pointee’s perspective), and... • An rvalue (from the pointer variable’s perspective). ## Exercise struct linked_list { node *head;0 码力 | 49 页 | 575.61 KB | 1 年前3
Message Handling with Boolean Algebra(Luke Valenty) Assigned to: elbeno (Ben Deane) Factor arbitrary matcher expressions into "sum-of-products" expressions ## Part 1 Fields and messages: how they are structured and specified. ## ## Boolean algebra primer/refresher In upcoming slides you'll see some Boolean expressions (not necessarily in C++). Just so we're all clear, I'm using this convention: • ^ means simple as possible. ## Example matcher problems We've made it easy for people to write complex expressions, possibly containing tautologies or contradictions, or at least common parts... How does the library0 码力 | 103 页 | 4.37 MB | 1 年前3
Back to Basics: C++ Templates - Part 2/9/6/a396eece687d6cc18563188e015082da/p2_1.jpg) Andreas Fertig B2B: C++ Templates ## Fold Expressions  ■ Used to unpack Print("Hello", "C++", 20); 15 } ## Fold Expressions 10 Normalize functions for 'normal' strings 2 auto Normalize(const std::string& t) /9/6/a396eece687d6cc18563188e015082da/p3_1.jpg) Andreas Fertig B2B: C++ Templates ## Fold Expressions 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 260 码力 | 12 页 | 787.22 KB | 1 年前3
DEDUCING this PATTERNSway to tell from within a member function whether the expression it’s invoked on is an lvalue or an rvalue; whether it is const or volatile; and the expression’s type." ## OVERVIEW • A bit about the &&; // plus all the overloads we seldom bother to write, yes you know the ones... // const rvalue refs? maybe... // volatile and const volatile, anyone? }; ## THE OBVIOUS WIN ## The first obvious my_vector { sort(self.begin(), self.end(), comp); return self; } }; // here's an rvalue that is moved through the computation my_vector{3,1,4,1,5,9,2,6,5}.sorted_by(less_than); We can0 码力 | 126 页 | 5.15 MB | 1 年前3
Forwarding References24923ef98d23975067156359/p9_1.jpg) ## V alue categories LVALUE glvalue that is not an xvalue RVALUE prvalue or an xvalue ; int i = 42; const int ci = 42; int make_int() { return 42; // ??? f(std::move(i)); // ??? f(std::move(ci)); // ??? f(42); // ???? f(make_int()); // ???? ## rvalue references void f(int&& ref); int i = 42; const int ci = 42; int make_int() { return0 码力 | 107 页 | 3.72 MB | 1 年前3
Back To Basics: Rvalues and Move Semanticsstr2 = str1; // (a) do not move std::string str3 = str2 + str1; // (b) move • Both assigned expressions are references ## So, how can we distinguish between the cases? std::string str2 = str1; // (a) (a) do not move std::string str3 = str2 + str1; // (b) move • Both assigned expressions are references - Currently, both lines would call the copy constructor ## So, how can we distinguish between the str2 = str1; // (a) do not move std::string str3 = str2 + str1; // (b) move • Both assigned expressions are references - Before C++11, both lines would call the copy constructor - But they are kind0 码力 | 80 页 | 740.53 KB | 1 年前3
Back to Basics: Move SemanticsC++ ©2021 by josuttis.com 15 josuttis | eckstein IT communication C++ Move Semantics ## RValue References C++ ©2021 by josuttis.com 16 ## V ectors Without Move Semantics (C++03) C++ josuttis | eckstein ## V ectors With Move Semantics (C++11) • With rvalue references you can provide move semantics • Rvalue references bind to rvalues – Caller no longer needs the value – May steal push_back(data()); // move temporary into coll coll.push_back(s+s); // move temporary into coll declares rvalue reference coll.push_back(std::move(s)); // move s into coll // (no longer need s) C++ 18 josuttis0 码力 | 23 页 | 1020.10 KB | 1 年前3
Back to Basics: Move Semanticsobject is an rvalue • Can result in more efficient code ## Move Semantics Quick summary Rvalue references, T\&\&, only bind to rvalues std::move turns any expression into an rvalue Move constructor constructor / assignment: One rvalue reference parameter Transfer ownership of resources Leave the source object in a valid state RVALUE REFERENCES ## Lvalue An lvalue is, roughly, something: • that can '\0'; std::arraywidgets; draw( widgets[123]); ## Rvalue An rvalue is, roughly, anything that is not an lvalue • Temporary objects • Literal constants • Function0 码力 | 142 页 | 1.02 MB | 1 年前3
共 1000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 100
相关搜索词
lambda表达式函数指针函数对象闭包通用lambdapolymorphismsmart pointerstemplatesconceptsC++reference_wrapperstd::functionlvalue referencesrvalue expressionsnullableBoolean algebramessage handlingmatchersexpression templatesconstraint expressionsvariadic templatesfold expressionstemplate template parametersvariable templatesSFINAEDeducing thistemplate deductionmember functionvalue categorylvaluervalue完美转发转发引用std::forwardmove semanticsstd::moveright value referencemove constructorMove Semanticsrvalue referencesperfect forwardingspecial member functionsmove assignmentunique_ptr













