-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeSnap-LambdaCapture.cpp.html
150 lines (146 loc) · 5.45 KB
/
CodeSnap-LambdaCapture.cpp.html
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<!----------------------------------------------------------------------------
CodeSnap-Lambda.cs.htm
Published 19 Mar 2017
Jim Fawcett, CSE687 : Object Oriented Design, Summer 2017
Note:
- Markup characters in the text part, enclosed in <pre>...</pre> have to be
replaced with escape sequences, e.g., < becomes < and > becomes >
- Be careful that you don't replace genuine markup characters with escape
sequences, e.g., everything outside of the <pre>...</pre> section.
----------------------------------------------------------------------------->
<html>
<head>
<script src="js/ScriptsUtilities.js"></script>
<script src="js/ScriptsTemplate.js"></script>
<script src="js/ScriptsKeyboard.js"></script>
<script src="js/ScriptsMenuCpp.js"></script>
<link rel="stylesheet" href="css/StylesTemplate.css" />
<link rel="stylesheet" href="css/StylesMenu.css" />
<style>
h3 {
font-weight: normal;
}
</style>
</head>
<body id="github" onload="initializeMenu()">
<nav>
<div id="navbar"></div>
</nav>
<a id="Next" href="CodeSnap-LambdaCapture.txt.html">N</a>
<a id="Prev" href="CodeSnap-LambdaCapture.txt.html">P</a>
<navKeys-Container>
<nav-Key id="sKey" onclick="toggleSwipeEvents()">S</nav-Key>
<nav-Key id="rKey" onclick="location.reload()">R</nav-Key>
<nav-Key id="tKey" onclick="scrollPageTop()">T</nav-Key>
<nav-Key id="bKey" onclick="scrollPageBottom()">B</nav-Key>
<nav-Key id="hKey" onclick="helpWin()">H</nav-Key>
<nav-Key id="pKey" onclick="loadPrev()">P</nav-Key>
<nav-Key id="nKey" onclick="loadNext()">N</nav-Key>
</navKeys-Container>
<h3>
<a href="CodeSnap-LambdaCapture.cpp.html">Lambdas.cpp</a>,
<a href="CodeSnap-LambdaCapture.txt.html">Lambdas.txt</a>,
<a class="disable" href="*">Code folder</a>
</h3>
<div class="indent">
Illustrates lambdas and the several ways they capture local state.
</div>
<hr />
<pre class="codeSnap">
/////////////////////////////////////////////////////////////////////
// Lambdas.cpp - demonstrate lambda capture //
// //
// Jim Fawcett, CSE687 - Object Oriented Design, Spring 2015 //
/////////////////////////////////////////////////////////////////////
/*
* When compiled a lambda definition is translated into a functor
* with operator() defining the code of the lambda. Any referenced
* data from the local scope becomes member data of the functor and
* gets passed around with the lambda.
*
* Lambdas are defined like this:
* 1. [=] (arguments)
* { code to execute may include names of local data as well as arguments };
* The [=] means that local data, like i below, are captured by value.
* 2. [&] (arguments)
* { code to execute may include names of local data as well as arguments };
* The [&] means that local data is captured by reference so must be in
* scope when the lambda executes.
* 3. [localVar1, &localVar2] (arguments)
* { code to execute includes localVar1 by value and localVar2 by reference }
*/
#include <functional>
#include <iostream>
#include <sstream>
//----< create a lambda and bind to a std::function >----------------
/*
* Lambda copies captured values, e.g., [=]
*/
std::function<std::string()> CreateLambda1(int i)
{
// this std function converts an integer to a string
std::function<std::string()> f = [=]()
{
std::ostringstream convert;
convert << i; // lambda captures value of i
return convert.str();
};
return f;
}
//----< create a lambda and bind to a std::function >----------------
/*
* Lambda copies captured values, e.g., []
*/
std::function<std::string(int)> CreateLambda2()
{
// this std function converts an integer to a string
std::function<std::string(int)> f = [](int i)
{
std::ostringstream convert;
convert << i; // lambda captures value of i
return convert.str();
};
return f;
}
//----< create a lambda and bind to a std::function >----------------
/*
* Lambda captures a reference to i and the value of j, e.g., [&i, j]
*/
std::function<std::string()> CreateLambda3(int& i)
{
// this std function converts integers to a string
int j = 40;
std::function<std::string()> f = [&i, j]()
{
std::ostringstream convert;
convert << i + j; // lambda captures reference to
return convert.str(); // external i - would be an error
}; // if i was passed by value to
return f; // CreateLambda3(int i)
}
//----< create a lambda and bind to a std::function >----------------
/*
* Lambda captures a reference to i that will become invalid when
* returning from invocation and a copy of j which will be valid,
* e.g., [&i, j]
*/
std::function<std::string()> CreateLambda4()
{
// this std function converts integers to a string
int i = 2;
int j = 40;
std::function<std::string()> f = [&i, j]()
{
std::ostringstream convert;
convert << i + j; // error here - will return invalid reference
return convert.str(); // to temporary value of i along with valid
}; // copy of j, e.g., [&i, j]
return f;
}
</pre>
<!--<div class="photo">
<img src="pictures/CSTstrip.jpg" width="100%" />
</div>-->
<info-bar></info-bar>
</body>
</html>