1.. _Lambda_Expressions:
2
3Lambda Expressions
4==================
5
6
7C++11 lambda expressions make the |full_name|
8``parallel_for`` much easier to use. A lambda expression lets
9the compiler do the tedious work of creating a function object.
10
11
12Below is the example from the previous section, rewritten with a lambda
13expression. The lambda expression, replaces both the declaration and construction of function object ``ApplyFoo`` in the
14example of the previous section.
15
16
17::
18
19
20   #include "oneapi/tbb.h"
21    
22
23   using namespace oneapi::tbb;
24    
25
26   void ParallelApplyFoo( float* a, size_t n ) {
27      parallel_for( blocked_range<size_t>(0,n),
28         [=](const blocked_range<size_t>& r) {
29                         for(size_t i=r.begin(); i!=r.end(); ++i)
30                             Foo(a[i]);
31                     }
32       );
33   }
34
35
36The [=] introduces the lambda expression. The expression creates a
37function object very similar to ``ApplyFoo``. When local variables like
38``a`` and ``n`` are declared outside the lambda expression, but used
39inside it, they are "captured" as fields inside the function object. The
40[=] specifies that capture is by value. Writing [&] instead would
41capture the values by reference. After the [=] is the parameter list and
42definition for the ``operator()`` of the generated function object. The
43compiler documentation says more about lambda expressions and other
44implemented C++11 features. It is worth reading more complete
45descriptions of lambda expressions than can fit here, because lambda
46expressions are a powerful feature for using template libraries in
47general.
48
49
50C++11 support is off by default in the compiler. The following table
51shows the option for turning it on.
52
53
54.. container:: tablenoborder
55
56
57   .. list-table::
58      :header-rows: 1
59
60      * -     Environment
61        -     Intel® C++ Compiler Classic
62        -     Intel® oneAPI DPC++/C++ Compiler
63      * -     Windows\* OS systems
64        -     \ ``icl /Qstd=c++11 foo.cpp``
65        -     \ ``icx /Qstd=c++11 foo.cpp``
66      * -     Linux\* OS systems
67        -     \ ``icc -std=c++11 foo.cpp``
68        -     \ ``icx -std=c++11 foo.cpp``
69
70
71
72
73For further compactness, oneTBB has a form of ``parallel_for`` expressly
74for parallel looping over a consecutive range of integers. The
75expression ``parallel_for(first,last,step,f)`` is like writing
76``for(auto i=first;         i<last;       i+=step)f(i)`` except that
77each f(i) can be evaluated in parallel if resources permit. The ``step``
78parameter is optional. Here is the previous example rewritten in the
79compact form:
80
81
82::
83
84
85   #include "oneapi/tbb.h"
86    
87
88   using namespace oneapi::tbb;
89    
90
91   #pragma warning(disable: 588)
92    
93
94   void ParallelApplyFoo(float a[], size_t n) {
95       parallel_for(size_t(0), n, [=](size_t i) {Foo(a[i]);});
96   }
97
98
99The compact form supports only unidimensional iteration spaces of
100integers and the automatic chunking feature detailed on the following
101section.
102
103