4. Using the library
PrevŽ | Ž | Ž Next

## 4. Using the library

¤³¤Î¥»¥¯¥·¥ç¥ó¤ÎÌÜÅª¤Ï¦Ë¥é¥¤¥Ö¥é¥ê¤Î´ðËÜÅª¤Êµ¡Ç½¤ò¾Ò²ð¤¹¤ë¤³¤È¤Ç¤¢¤ë¡£ Îã³°»ö¹à¤äÆÃÊÌ¤Ê¾ì¹ç¤âÈó¾ï¤Ë¤¿¤¯¤µ¤ó¤¢¤ë¤¬¡¢¤½¤ì¤é¤ÎµÄÏÀ¤Ï¸å¤Î¥»¥¯¥·¥ç¥ó¤Ë¤Þ¤ï¤¹¤³¤È¤Ë¤¹¤ë¡£

The purpose of this section is to introduce the basic functionality of the library. There are quite a lot of exceptions and special cases, but discussion of them is postponed until later sections.

### 4.1. Introductory Examples

¤³¤Î¥»¥¯¥·¥ç¥ó¤Ç¤Ï¡¢STL ¥¢¥ë¥´¥ê¥º¥à¤Î¸Æ½Ð¤·¤Ë¤ª¤±¤ë¡¢BLL ¤Î¦Ë¼°¤Î´ðËÜÅª¤ÊÎã¤ò¼¨¤¹¡£ ¤¤¤¯¤Ä¤«¤ÎÃ±½ã¤Ê¼°¤«¤é»Ï¤á¤Æ¡¢½ù¡¹¤ËÈ¯Å¸¤µ¤»¤Æ¤¤¤¯¡£ ¤Þ¤º¡¢¥³¥ó¥Æ¥Ê( Îã¤¨¤Ð `list` ) ¤ÎÍ×ÁÇ¤ò `1` ¤Ë½é´ü²½¤·¤Æ¤ß¤ë¡£

    list<int> v(10);
    for_each(v.begin(), v.end(), _1 = 1);

`_1 = 1` ¤È¤¤¤¦¼°¤Ï¡¢`v` ¤ÎÁ´¤Æ¤ÎÍ×ÁÇ¤ËÂÐ¤·¤Æ 1 ¤ò³äÅö¤Æ¤ë¦Ë¥Õ¥¡¥ó¥¯¥¿¤òÀ¸À®¤¹¤ë¡£ [1]

In this section we give basic examples of using BLL lambda expressions in STL algorithm invocations. We start with some simple expressions and work up. First, we initialize the elements of a container, say, a `list`, to the value `1`:

    list<int> v(10);
    for_each(v.begin(), v.end(), _1 = 1);

The expression `_1 = 1` creates a lambda functor which assigns the value `1` to every element in `v`.[1]

¼¡¤Ë¡¢¥Ý¥¤¥ó¥¿¤Î¥³¥ó¥Æ¥Ê¤òºîÀ®¤·¡¢¤½¤Î¥Ý¥¤¥ó¥¿¤òºÇ½é¤Î¥³¥ó¥Æ¥Ê `v` ¤ÎÍ×ÁÇ¤ò»Ø¤¹¤è¤¦¤Ë¤¹¤ë¡£

    vector<int*> vp(10);
    transform(v.begin(), v.end(), vp.begin(), &_1);

`&_1`¤È¤¤¤¦¼°¤Ï¡¢`v`¤Î¤½¤ì¤¾¤ì¤ÎÍ×ÁÇ¤Î¥¢¥É¥ì¥¹¤ò¼èÆÀ¤¹¤ë´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤òÀ¸À®¤¹¤ë¡£ ¤½¤·¤Æ¡¢¤½¤Î¥¢¥É¥ì¥¹¤ÏÂÐ±þ¤¹¤ë`vp`¤ÎÍ×ÁÇ¤Ë³äÅö¤Æ¤é¤ì¤ë¡£

Next, we create a container of pointers and make them point to the elements in the first container `v`:

    vector<int*> vp(10);
    transform(v.begin(), v.end(), vp.begin(), &_1);

The expression `&_1` creates a function object for getting the address of each element in `v`. The addresses get assigned to the corresponding elements in `vp`.

¼¡¤Î¥³¡¼¥É¤Ï `v` ¤ÎÍ×ÁÇ¤ÎÃÍ¤òÊÑ²½¤µ¤»¤ë¡£ ¤½¤ì¤¾¤ì¤ÎÍ×ÁÇ¤ËÂÐ¤·¤Æ¡¢´Ø¿ô`foo` ¤¬¸Æ¤Ð¤ì¤ë¡£ `foo`¤Ë¤Ï¡¢³ÆÍ×ÁÇ¤ÎÃÍ¤¬°ú¿ô¤È¤·¤ÆÅÏ¤µ¤ì¡¢¤½¤Î·ë²Ì¤Ï°ú¿ô¤È¤·¤ÆÅÏ¤µ¤ì¤¿Í×ÁÇ¤Ë³äÅö¤Æ¤é¤ì¤ë¡£

    int foo(int);
    for_each(v.begin(), v.end(), _1 = bind(foo, _1));

The next code fragment changes the values in `v`. For each element, the function `foo` is called. The original value of the element is passed as an argument to `foo`. The result of `foo` is assigned back to the element:

    int foo(int);
    for_each(v.begin(), v.end(), _1 = bind(foo, _1));

¼¡¤Ç¤Ï¡¢`vp` ¤ÎÍ×ÁÇ¤Î¥½¡¼¥È¤ò¹Ô¤¦¡£

    sort(vp.begin(), vp.end(), *_1 > *_2);

¤³¤Î`sort`¤Î¸Æ½Ð¤·¤Ç¤Ï¡¢Í×ÁÇ¤ò¥Ý¥¤¥ó¥¿¤Î»Ø¤¹ÆâÍÆ¤Î¹ß½ç¤Ç¥½¡¼¥È¤¹¤ë¡£

The next step is to sort the elements of `vp`:

    sort(vp.begin(), vp.end(), *_1 > *_2);

In this call to `sort`, we are sorting the elements by their contents in descending order.

ºÇ¸å¤Ë¡¢¼¡¤Î `for_each` ¤Î¸Æ½Ð¤·¤Ç¤Ï¡¢¥½¡¼¥È¤µ¤ì¤¿ `vp` ¤ÎÍ×ÁÇ¤Î»Ø¤¹ÆâÍÆ¤ò²þ¹Ô¶èÀÚ¤ê¤Ç½ÐÎÏ¤¹¤ë¡£

    for_each(vp.begin(), vp.end(), cout << *_1 << '\n');

¦Ë¼°¤ÎÉôÊ¬¼°¤ÇÄÌ¾ï¤Î¼°( ¦Ë¼°¤Ç¤Ê¤¤ )¤â¤Î¤ÏÂ¨ºÂ¤ËÉ¾²Á¤µ¤ì¤ë¤³¤È¤ËÃí°Õ¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£ ¤½¤¦¤·¤Ê¤¤¤È¡¢Í½´ü¤·¤Ê¤¤·ë²Ì¤òÆ³¤¤¤Æ¤·¤Þ¤¦¡£ Îã¤¨¤Ð¡¢Àè¤ÎÎã¤Ï¼¡¤Î¤è¤¦¤Ë½ñ´¹¤¨¤Æ¤ß¤ë¤È¡¢

    for_each(vp.begin(), vp.end(), cout << '\n' << *_1);

ÉôÊ¬¼°`cout << << '\n'` ¤ÏÂ¨ºÂ¤ËÉ¾²Á¤µ¤ì¤Æ¤·¤Þ¤¦¡£ ¤½¤Î·ë²Ì¡¢°ì¤Ä¤Î²þ¹Ô¤È¤½¤ì¤ËÂ³¤¤¤Æ `vp` ¤ÎÍ×ÁÇ¤¬½ÐÎÏ¤µ¤ì¤Æ¤·¤Þ¤¦¡£ BLL ¤Ç¤Ï´Ø¿ô `constant` ¤È´Ø¿ô `var` ¤¬Äó¶¡¤µ¤ì¤Æ¤ª¤ê¡¢¤½¤ì¤¾¤ìÄê¿ô¤ÈÊÑ¿ô¤ò¦Ë¼°¤Ø¤ÈÊÑ´¹¤¹¤ë¡£ ¤³¤ì¤é¤Î´Ø¿ô¤ò»ÈÍÑ¤¹¤ë¤³¤È¤Ë¤è¤ê¡¢ÉôÊ¬¼°¤¬Â¨ºÂ¤ËÉ¾²Á¤µ¤ì¤Æ¤·¤Þ¤¦¤³¤È¤òËÉ»ß¤Ç¤¤ë¡£

    for_each(vp.begin(), vp.end(), cout << constant('\n') << *_1);

¤³¤ì¤é¤Î´Ø¿ô¤Ï Section 5.5 ¤Ç¤è¤ê¾Ü¤·¤¯µ½Ò¤¹¤ë¡£

Finally, the following `for_each` call outputs the sorted content of `vp` separated by line breaks:

    for_each(vp.begin(), vp.end(), cout << *_1 << '\n');

Note that a normal (non-lambda) expression as subexpression of a lambda expression is evaluated immediately. This may cause surprises. For instance, if the previous example is rewritten as

    for_each(vp.begin(), vp.end(), cout << '\n' << *_1);

the subexpression `cout << '\n'` is evaluated immediately and the effect is to output a single line break, followed by the elements of `vp`. The BLL provides functions `constant` and `var` to turn constants and, respectively, variables into lambda expressions, and can be used to prevent the immediate evaluation of subexpressions:

    for_each(vp.begin(), vp.end(), cout << constant('\n') << *_1);

These functions are described more thoroughly in Section 5.5

### 4.2. Parameter and return types of lambda functors

¦Ë¥Õ¥¡¥ó¥¯¥¿¤Î¸Æ½Ð¤·¤Î´Ö¤Ï¡¢¼Â°ú¿ô¤Ï¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼¤È¤·¤ÆÃê¾Ý²½¤µ¤ì¤Æ¤¤¤ë¡£ ¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼¤Ï¼Â°ú¿ô¤Î·¿¤Ë¤Ï±Æ¶Á¤òÍ¿¤¨¤Ê¤¤¡£ ´ðËÜÅª¤Ë¤Ï¡¢Ãê¾Ý²½¤µ¤ì¤¿¦Ë¼°¤¬ C++ ¤ÎÍ¸ú¤Ê¼°¤Ç¤¢¤ë¤«¤®¤ê¡¢¦Ë´Ø¿ô¤Ï¤¤¤«¤Ê¤ë·¿¤Î°ú¿ô¤ò¤È¤â¤Ê¤Ã¤Æ¤â¸Æ¤Ö¤³¤È¤¬¤Ç¤¤ë¡£ Îã¤¨¤Ð¡¢ `_1 + _2` ¤Ï¡¢Æó¹à¦Ë¥Õ¥¡¥ó¥¯¥¿¤òÀ¸À®¤¹¤ë¡£ ¤³¤Î¥Õ¥¡¥ó¥¯¥¿¤Ï¤¤¤«¤Ê¤ë·¿ `A` ¤È ·¿`B` ¤ÎÆó¤Ä¤Î¥ª¥Ö¥¸¥§ ¥¯¥È¤È¶¦¤Ë¸Æ½Ð¤¹¤³¤È¤¬¤Ç¤¤ë¡£¤³¤ì¤é¤Î·¿¤Ë¤Ï `operator+(A,B)` ¤¬ÄêµÁ¤µ¤ì¤Æ¤¤¤ì¤Ð¤è¤¤¡£(¤½¤·¤Æ¡¢BLL ¤¬¤½¤Î±é»»»Ò¤ÎÊÖ¤êÃÍ¤ò¼±ÊÌ¤Ç¤¤ì ¤Ð¤è¤¤¡£¾Ü¤·¤¯¤Ï°Ê²¼¤ò¸«¤è¡£)

During the invocation of a lambda functor, the actual arguments are substituted for the placeholders. The placeholders do not dictate the type of these actual arguments. The basic rule is that a lambda function can be called with arguments of any types, as long as the lambda expression with substitutions performed is a valid C++ expression. As an example, the expression `_1 + _2` creates a binary lambda functor. It can be called with two objects of any types `A` and `B` for which `operator+(A,B)` is defined (and for which BLL knows the return type of the operator, see below).

C++ ¤Ë¤Ï¼°¤Î·¿¤òÌä¹ç¤»¤ë¥á¥«¥Ë¥º¥à¤¬Â¸ºß¤·¤Ê¤¤¡£ ¤·¤«¤·¡¢¤³¤ÎÀµ³Î¤Êµ¡Ç½¤Ï C++ ¤Î¦Ë¼°¤Î¼ÂÁõ¤Ë¤Ï¶Ë¤á¤Æ½ÅÂç¤Ç¤¢¤ë¡£ ¤è¤Ã¤Æ¡¢BLL ¤Ï¤Á¤ç¤Ã¤È¤·¤¿Ê£»¨¤Ê·¿¿äÏÀ¥·¥¹¥Æ¥à¤òÆâÊñ¤·¤Æ¤¤¤ë¡£ ¤½¤³¤Ç¤Ï¡¢¦Ë´Ø¿ô¤ÎÊÖ¤êÃÍ¤Î·¿¤ò¿äÏÀ¤¹¤ë¤¿¤á¤Ë¡¢ÆÃÀ¥¯¥é¥¹¤Î½¸¹ç¤ò»ÈÍÑ¤·¤Æ¤¤¤ë¡£ ¤½¤Î¿äÏÀ¥·¥¹¥Æ¥à¤ÏÁÈ¤ß¹þ¤ß·¿¤ä¡¢É¸½à¥é¥¤¥Ö¥é¥ê¤Î·¿¤¬¥ª¥Ú¥é¥ó¥É¤Ç¤¢¤ëÂ¿¤¯¤Î¼°¤¬¥ª¥Ú¥é¥ó¥É ¤Ç¤¢¤ë¼°¤ò°·¤¦¡£ Â¿¤¯¤Î¥æ¡¼¥¶ÄêµÁ·¿¤â¡¢ÊÖ¤êÃÍ¤Î·¿¤Î·èÄê¤Ë¤ª¤±¤ëÄÌ¾ï¤Î´·Îã¤Ë½¾¤Ã¤Æ¥æ¡¼¥¶ ¤¬±é»»»Ò¤òÄêµÁ¤·¤Æ¤¤¤ë¸Â¤ê¤Ï¡¢¤¦¤Þ¤¯ÂÐ±þ¤Ç¤¤Æ¤¤¤ë¡£

C++ lacks a mechanism to query a type of an expression. However, this precise mechanism is crucial for the implementation of C++ lambda expressions. Consequently, BLL includes a somewhat complex type deduction system which uses a set of traits classes for deducing the resulting type of lambda functions. It handles expressions where the operands are of built-in types and many of the expressions with operands of standard library types. Many of the user defined types are covered as well, particularly if the user defined operators obey normal conventions in defining the return types.

¤·¤«¤·¤Ê¤¬¤é¡¢ÊÖ¤êÃÍ¤Î·¿¤ò¿äÏÀ¤Ç¤¤Ê¤¤¾ì¹ç¤¬Â¸ºß¤¹¤ë¡£ Îã¤¨¤Ð¡¢¼¡¤Î¤è¤¦¤ËÄêµÁ¤ò¤·¤¿¤È¤¹¤ë¤È¡¢

    C operator+(A, B);

¼¡¤Î¦Ë¥Õ¥¡¥ó¥¯¥¿¤Î¸Æ½Ð¤·¤Ï¡¢ÊÖ¤êÃÍ¤Î·¿¤ò¿äÏÀ¤Ç¤¤Ê¤¤¤¿¤á¤Ë¼ºÇÔ¤¹¤ë¡£

    A a; B b; (_1 + _2)(a, b);

There are, however, cases when the return type cannot be deduced. For example, suppose you have defined:

    C operator+(A, B);

The following lambda function invocation fails, since the return type cannot be deduced:

    A a; B b; (_1 + _2)(a, b);

¤³¤ì¤ò²ò·è¤¹¤ë¤Ë¤ÏÆó¤Ä¤ÎÂåÂØ°Æ¤¬¤¢¤ë¡£ °ì¤ÄÌÜ¤Ï¡¢BLL ¤Î·¿¿äÏÀ¥·¥¹¥Æ¥à¤òÆÈ¼«¤Î·¿¤ËÂÐ±þ¤¹¤ë¤¿¤á¤Ë³ÈÄ¥¤¹¤ë¤³¤È¤Ç ¤¢¤ë¡£ (¾Ü¤·¤¯¤Ï Section 6 ¤ò¸«¤è) Æó¤ÄÌÜ¤ÎÊýË¡¤Ï¼¡¤Î¤è¤¦¤ËÆÃÊÌ¤Ê¦Ë¼° `ret`¤ò»ÈÍÑ¤¹¤ë¤³¤È¤Ç¤¢¤ë¡£

    A a; B b; ret<C>(_1 + _2)(a, b);

¤³¤Î¦Ë¼°¤Ï·¿¿äÏÀ¥·¥¹¥Æ¥à¤ËÂå¤ï¤Ã¤Æ¡¢ÊÖ¤êÃÍ¤Î·¿¤ò·èÄê¤¹¤ë¡£ (¾Ü¤·¤¯¤Ï Section 5.4 ¤ò¸«¤è)

There are two alternative solutions to this. The first is to extend the BLL type deduction system to cover your own types (see Section 6). The second is to use a special lambda expression (`ret`) which defines the return type in place (see Section 5.4):

    A a; B b; ret<C>(_1 + _2)(a, b);

bind ¼°¤Ë´Ø¤·¤Æ¤Ï¡¢ÊÖ¤êÃÍ¤Î·¿¤Ï¥Æ¥ó¥×¥ì¡¼¥È°ú¿ô¤È¤·¤Æ¼¡¤Î¤è¤¦¤Ë»ØÄê¤¹ ¤ë¤³¤È¤¬¤Ç¤¤ë¡£

    bind<int>(foo, _1, _2);

For bind expressions, the return type can be defined as a template argument of the bind function as well:

    bind<int>(foo, _1, _2);

### 4.3. About actual arguments to lambda functors

°ìÈÌÅª¤Ê¼Â°ú¿ô¤ÎÀ©¸Â¤Ï¡¢¼Â°ú¿ô¤Ï const¤Ç¤Ê¤¤±¦ÊÕÃÍ¤Ç¤Ï¤Ê¤¤¤È¤¤¤¦¤³¤È¤Ç¤¢¤ë¡£ Îã¤¨¤Ð¡¢

    int i = 1; int j = 2;
    (_1 + _2)(i, j); // ok
    (_1 + _2)(1, 2); // error (!)

¤Ç¤¢¤ë¡£ ¤³¤ÎÀ©¸Â¤Ï¤½¤ì¤Û¤É°¤¤¤â¤Î¤Ç¤Ï¤Ê¤¤¡£ ¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ï¤¿¤¤¤Æ¤¤¤Î¾ì¹ç¡¢STL ¤Î¥¢¥ë¥´¥ê¥º¥àÃæ¤Ç¸Æ½Ð¤µ¤ì¤ë¤¿¤á¡¢°ú¿ô¤Ï»²¾È³°¤·¤Î¥¤¥Æ¥ì¡¼¥¿¤Ç¤¢¤ê¡¢»²¾È³°¤·¤ÎÁàºî¤¬±¦ÊÕÃÍ¤òÊÖ¤¹¤³¤È¤Ï¤Û¤È¤ó¤É¤Ê¤¤¤«¤é¤Ç¤¢¤ë¡£ ¤½¤·¤Æ¡¢±¦ÊÕÃÍ¤òÊÖ¤¹¤è¤¦¤Ê¾ì¹ç¤Ë¤Ï¡¢Section 5.9.2¤Ç½Ò¤Ù¤ë¤è¤¦¤ÊÂåÂØ°Æ¤¬¤¢¤ë¡£

A general restriction for the actual arguments is that they cannot be non-const rvalues. For example:

    int i = 1; int j = 2;
    (_1 + _2)(i, j); // ok
    (_1 + _2)(1, 2); // error (!)

This restriction is not as bad as it may look. Since the lambda functors are most often called inside STL-algorithms, the arguments originate from dereferencing iterators and the dereferencing operators seldom return rvalues. And for the cases where they do, there are workarounds discussed in Section 5.9.2.

### 4.4. Storing bound arguments in lambda functions

¥Ç¥Õ¥©¥ë¥È¤ÎÆ°ºî¤Ç¤Ï¡¢Â«Çû¤µ¤ì¤¿°ú¿ô¤Î const ¤Ê°ì»þÅª¤Ê¥³¥Ô¡¼¤Ï¡¢¦Ë¥Õ¥¡¥ó¥¯¥¿Ãæ¤Ë³ÊÇ¼¤µ¤ì¤ë¡£ ¤Ä¤Þ¤ê¡¢Â«Çû¤µ¤ì¤¿°ú¿ô¤ÎÃÍ¤Ï¡¢¦Ë´Ø¿ô¤ÎÀ¸À®»þ¤Ë¸ÇÄê¤µ¤ì¡¢¤«¤Ä¡¢¦Ë´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤Î¥é¥¤¥Õ¥¿¥¤¥à¤Î´Ö¤ÏÄê¿ô¤Ç¤¢¤êÂ³¤±¤ë¤È¤¤¤¦¤³¤È¤Ç¤¢¤ë¡£ Îã¤¨¤Ð¡¢

    int i = 1;
    (_1 + i)(i = 2);

ºÇ¸å¤Î¹Ô¤Î¼°¤ÎÃÍ¤Ï 4 ¤Ç¤Ï¤Ê¤¯¡¢3 ¤Ç¤¢¤ë¡£ ¸À¤¤´¹¤¨¤ë¤È¡¢¦Ë¼° `_1 + i`¤Ï `lambda x.x+i`¤Ç¤Ï¤Ê¤¯¡¢ `lambda x.x+1`¤È¤¤¤¦¦Ë´Ø¿ô¤òÀ¸À®¤¹¤ë¡£

By default, temporary const copies of the bound arguments are stored in the lambda functor. This means that the value of a bound argument is fixed at the time of the creation of the lambda function and remains constant during the lifetime of the lambda function object. For example:

    int i = 1;
    (_1 + i)(i = 2);

The value of the expression in the last line is 3, not 4. In other words, the lambda expression `_1 + i` creates a lambda function `lambda x.x+1` rather than `lambda x.x+i`.

Àè¤Ë½Ò¤Ù¤¿¤è¤¦¤Ë¡¢¤³¤ì¤Ï¤¢¤¯¤Þ¤Ç¥Ç¥Õ¥©¥ë¥È¤ÎÆ°ºî¤Ç¤¢¤Ã¤Æ¡¢Îã³°¤â¤¢¤ë¡£ Àµ³Î¤Êµ¬Â§¤Ï¼¡¤Î¤è¤¦¤Ë¤Ê¤ë¡£
As said, this is the default behavior for which there are exceptions. The exact rules are as follows:

- ¥×¥í¥°¥é¥Þ¤Ï`ref` ¤ä `cref` ¤Î¥é¥Ã¥Ñ[ref]¤ò»ÈÍÑ¤¹¤ë¤³¤È¤Ë¤è¤ê¡¢³ÊÇ¼¤Î¥á¥«¥Ë¥º¥à¤òÁàºî¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£ °ú¿ô¤ò `ref` ¤ä `cref`¤Ç¥é¥Ã¥×¤¹¤ë¤³¤È¤Ë¤è¤ê¡¢°ú¿ô¤ò¤½¤ì¤¾¤ì»²¾È¤È¤·¤Æ¡¢const ¤Ø¤Î»²¾È¤È¤·¤Æ³ÊÇ¼¤¹¤ë¤è¤¦¤Ë»ØÄê¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£ Îã¤¨¤Ð¡¢Àè¤ÎÎã¤ÎÊÑ¿ô `i` ¤ò `ref` ¤Ç¥é¥Ã¥×¤·¤Æ½ñ¤Ä¾¤»¤Ð¡¢ `lambda x.x+i`¤È¤¤¤¦¦Ë¼°¤òºîÀ®¤·¡¢ºÇ¸å¤Î¹Ô¤Î¼°¤ÎÃÍ¤Ï 4 ¤È¤Ê¤ë¡£

      i = 1;
      (_1 + ref(i))(i = 2);

  `ref` ¤È `cref` ¤Ï `var` ¤ä `constant` ¤È¤Ï°Û¤ë¤³¤È¤ËÃí°Õ¤·¤Ê¤µ¤¤¡£ ¸å¼Ô¤Ï¡¢¦Ë¥Õ¥¡¥ó¥¯¥¿¤òÀ¸À®¤¹¤ë¤Î¤ËÂÐ¤·¡¢Á°¼Ô¤Ï¤½¤¦¤Ç¤Ï¤Ê¤¤¡£ Îã¤¨¤Ð¡¢

      int i;
      var(i) = 1; // ok
      ref(i) = 1; // not ok, ref(i) is not a lambda functor

  ¤È¤Ê¤ë¡£ ´Ø¿ô`ref` ¤È `cref` ¤Ï¤Û¤È¤ó¤ÉÎò»ËÅª¤ÊÍýÍ³¤«¤é¤Î¤ßÂ¸ºß¤·¤Æ¤¤¤ë¡£ ¤½¤·¤Æ¡¢`ref` ¤Ï `var` ¤Ç `cref` ¤Ï `constant_ref`¤ÇÃÖ¤´¹¤¨¤ë¤³¤È¤¬¤Ç¤¤ë¡£ ¾Ü¤·¤¯¤Ï Section 5.5 ¤ò¸«¤è¡£ `ref` ¤È `cref` ¤Ï Boost ¤Î´ðËÜÅª¤ÊÌÜÅª¤Î¥æ¡¼¥Æ¥£¥ê¥Æ¥£¡¼´Ø¿ô¤Ç¤¢¤ë¤Î¤Ç¡¢`boost` ¥Í¡¼¥à¥¹¥Ú¡¼¥¹¤ËÄ¾ÀÜÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£
  The programmer can control the storing mechanism with `ref` and `cref` wrappers [ref]. Wrapping an argument with `ref`, or `cref`, instructs the library to store the argument as a reference, or as a reference to const respectively. For example, if we rewrite the previous example and wrap the variable `i` with `ref`, we are creating the lambda expression `lambda x.x+i` and the value of the expression in the last line will be 4:

      i = 1;
      (_1 + ref(i))(i = 2);

  Note that `ref` and `cref` are different from `var` and `constant`. While the latter ones create lambda functors, the former do not. For example:

      int i;
      var(i) = 1; // ok
      ref(i) = 1; // not ok, ref(i) is not a lambda functor

  The functions `ref` and `cref` mostly exist for historical reasons, and `ref` can always be replaced with `var`, and `cref` with `constant_ref`. See Section 5.5 for details. The `ref` and `cref` functions are general purpose utility functions in Boost, and hence defined directly in the `boost` namespace.

- ÇÛÎó·¿¤Ï¥³¥Ô¡¼¤¹¤ë¤³¤È¤Ï¤Ç¤¤º¡¢¥Ç¥Õ¥©¥ë¥È¤ÎÆ°ºî¤Ç¤Ï¡¢const ¤Ê»²¾È¤È¤·¤Æ³ÊÇ¼¤µ¤ì¤ë¡£

- Array types cannot be copied, they are thus stored as const reference by default.

- ¤¤¤¯¤Ä¤«¤Î¼°¤Ë¤Ä¤¤¤Æ¤Ï¡¢»²¾È¤È¤·¤Æ°ú¿ô¤ò³ÊÇ¼¤¹¤ë¤Û¤¦¤¬Ç¼ÆÀ¤¬¤¤¤¯¡£ Îã¤¨¤Ð¡¢¦Ë¼°`i += _1`¤Î°Õ¿Þ¤ÏÌÀ¤é¤«¤Ë¡¢¦Ë¥Õ¥¡¥ó¥¯¥¿¤Î¸Æ½Ð¤·¤Ë¤è¤ê¡¢°ì»þÅª¤Ê¥³¥Ô¡¼¤Ç¤Ï¤Ê¤¯¡¢ÊÑ¿ô`i`¤½¤Î¤â¤Î¤ÎÃÍ¤Ë±Æ¶Á¤òÍ¿¤¨¤ë¤³¤È¤Ç¤¢¤ë¡£ ÊÌ¤ÊÎã¤òµó¤²¤ë¤È¡¢¥¹¥È¥ê¡¼¥à¤Î±é»»»Ò¤Ï const ¤Ç¤Ê¤¤»²¾È¤È¤·¤ÆºÇº¸¤Î°ú¿ô¤ò¤È¤ë¡£ Àµ³Î¤Êµ¬Â§¤Ï¼¡¤Î¤è¤¦¤Ë¤Ê¤ë¡£
  For some expressions it makes more sense to store the arguments as references. For example, the obvious intention of the lambda expression `i += _1` is that calls to the lambda functor affect the value of the variable `i`, rather than some temporary copy of it. As another example, the streaming operators take their leftmost argument as non-const references. The exact rules are:

  - Ê£¹çÂåÆþ±é»»»Ò(`+=`¤ä`*=`¤Î¤è¤¦¤Ê¤â¤Î)¤Îº¸Â¦¤Î°ú¿ô¤Ï¡¢const ¤Ç¤Ê¤¤»²¾È¤È¤·¤Æ³ÊÇ¼¤µ¤ì¤ë¡£

  - The left argument of compound assignment operators (`+=`, `*=`, etc.) are stored as references to non-const.

  - `<<` ¤Îº¸¤Î°ú¿ô¤¬ `basic_ostream` ¤Î¥¤¥ó¥¹¥¿¥ó¥¹²½¤«¤éÇÉÀ¸¤·¤Æ¤¤¤¿¾ì¹ç¤ä¡¢ `>>` ¤Îº¸¤Î°ú¿ô¤¬ `basic_istream` ¤Î¥¤¥ó¥¹¥¿¥ó¥¹²½¤«¤éÇÉÀ¸¤·¤¿¾ì¹ç¤Ë¤Ï¡¢ ¤½¤Î°ú¿ô¤Ï const¤Ç¤Ê¤¤»²¾È¤È¤·¤Æ³ÊÇ¼¤µ¤ì¤ë¡£ ¤½¤ÎÂ¾¤Î·¿¤Ë´Ø¤·¤Æ¤Ï¡¢°ú¿ô¤Ï¥³¥Ô¡¼¤È¤·¤Æ³ÊÇ¼¤µ¤ì¤ë¡£

  - If the left argument of `<<` or `>>` operator is derived from an instantiation of `basic_ostream` or respectively from `basic_istream`, the argument is stored as a reference to non-const. For all other types, the argument is stored as a copy.

  - ¥Ý¥¤¥ó¥¿¤Î»»½Ñ¼°¤Ë¤ª¤¤¤Æ¤Ï¡¢const ¤Ç¤Ê¤¤ÇÛÎó·¿¤Ï¡¢const¤Ç¤Ê¤¤»²¾È¤È¤·¤Æ³ÊÇ¼¤µ¤ì¤ë¡£ ¤³¤ì¤Ï¡¢¥Ý¥¤¥ó¥¿¤Î»»½Ñ·×»»¤Ë¤è¤Ã¤Æ¡¢const ¤Ç¤Ê¤¤ÇÛÎó¤¬ const ¤Ë¤Ê¤ë¤Î¤òËÉ¤°¤¿¤á¤Ç¤¢¤ë¡£

  - In pointer arithmetic expressions, non-const array types are stored as non-const references. This is to prevent pointer arithmetic making non-const arrays const.

[1] ¸·Ì©¤Ë¤Ï¡¢C++ É¸½àµ¬³Ê¤Ç¤Ï¡¢`for_each` ¤Ï ÊÑ¹¹¤Î¤Ê¤¤Ï¢Â³Áàºî ¤Ç¤¢¤ê¡¢ `for_each`¤ØÅÏ¤¹´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤Ï¤½¤Î°ú¿ô¤ò¤òÊÑ¹¹¤¹¤Ù¤¤Ç¤Ê¤¤¤ÈÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£ ¤³¤Î°ú¿ô¤ËÂÐ¤¹¤ëÍ×µá¤ÏÉÔÉ¬Í×¤Ë¸·¤·¤¹¤®¤ë¡£ ¤Ê¤¼¤Ê¤é¡¢¥¤¥Æ¥ì¡¼¥¿¤¬ mutable ¤Ç¤¢¤ë¸Â¤ê¡¢`for_each` ¤Ï°ú¿ô¤Ø¤ÎÉûºîÍÑ¤Î¤¢¤ë´Ø¿ô¥ª¥Ö¥¸¥å¥¯¥È¤â½èÍý¤Ç¤¤ë¤«¤é¤Ç¤¢¤ë¡£ ¤½¤ì¤Ç¤â¤ä¤Ï¤ê¡¢`std::for_each` ¤Îµ¡Ç½¤ò»ý¤Ä¤¬¡¢°ú¿ô¤Ë´Ø¤¹¤ë°ìÁØ¤¤á¤ÎºÙ¤«¤¤Í×µá¤Î´Ø¿ô¥Æ¥ó¥×¥ì¡¼¥È¤òÄó¶¡¤¹¤ë¤³¤È¤Ï´ÊÃ±¤Ç¤¢¤ë¡£
Strictly taken, the C++ standard defines `for_each` as a non-modifying sequence operation, and the function object passed to `for_each` should not modify its argument. The requirements for the arguments of `for_each` are unnecessary strict, since as long as the iterators are mutable, `for_each` accepts a function object that can have side-effects on their argument. Nevertheless, it is straightforward to provide another function template with the functionality of`std::for_each` but more fine-grained requirements for its arguments.

PrevŽ | Up | Ž Next
3. IntroductionŽ | Home | Ž 5. Lambda expressions in details