5. Lambda expressions in details
PrevŽ | Ž | Ž Next

## 5. Lambda expressions in details

¤³¤Î¥»¥¯¥·¥ç¥ó¤Ç¤ÏÍÍ¡¹¤Ê·Á¤Î¦Ë¼°¤ò¾Ü¤·¤¯ÀâÌÀ¤¹¤ë¡£ ¸Ä¡¹¤Î¥µ¥Ö¥»¥¯¥·¥ç¥ó¤Ç¤Ï¤½¤ì¤¾¤ìÍ¸ú¤Ê¦Ë¼°¤Î·Á¼°¤Ë¤Ä¤¤¤ÆÀâÌÀ¤·¤Æ¤¤¤ë¡£

This section describes different categories of lambda expressions in details. We devote a separate section for each of the possible forms of a lambda expression.

### 5.1. Placeholders

BLL ¤Ç¤Ï¡¢`placeholder1_type`¡¢`placeholder2_type`¡¢`placeholder3_type`¤È¤¤¤¦ 3 ¤Ä¤Î¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼·¿¤òÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£ ¤Þ¤¿¡¢BLL ¤Ç¤Ï¤½¤ì¤¾¤ì¤Î¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼·¿¤ÎÊÑ¿ô¡¢`_1`, `_2` and `_3` ¤¬¤¢¤é¤«¤¸¤áÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£ ¤·¤«¤·¡¢¥æ¡¼¥¶¤Ï¤³¤ì¤é¤Î¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼¤ò»ÈÍÑ¤·¤Ê¤¯¤È¤â¤è¤¤¡£ ÊÌ¤ÊÌ¾Á°¤Î¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼¤Ï´ÊÃ±¤ËÄêµÁ¤Ç¤¤ë¡£ ¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼·¿¤Î¿·¤·¤¤ÊÑ¿ô¤òÄêµÁ¤¹¤ì¤Ð¤è¤¤¡£ Îã¤¨¤Ð¡¢¼¡¤Î¤è¤¦¤Ë¤Ê¤ë¡£

    boost::lambda::placeholder1_type X;
    boost::lambda::placeholder2_type Y;
    boost::lambda::placeholder3_type Z;

¤³¤ì¤é¤ÎÊÑ¿ô¤òÄêµÁ¤¹¤ì¤Ð¡¢`X += Y * Z` ¤Ï `_1 += _2 * _3` ¤ÈÅù²Á¤Ç¤¢¤ë¡£

The BLL defines three placeholder types: `placeholder1_type`, `placeholder2_type` and `placeholder3_type`. BLL has a predefined placeholder variable for each placeholder type: `_1`, `_2` and `_3`. However, the user is not forced to use these placeholders. It is easy to define placeholders with alternative names. This is done by defining new variables of placeholder types. For example:

    boost::lambda::placeholder1_type X;
    boost::lambda::placeholder2_type Y;
    boost::lambda::placeholder3_type Z;

With these variables defined, `X += Y * Z` is equivalent to `_1 += _2 * _3`.

¦Ë¼°¤Ç¤Î¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼¤Î»ÈÍÑË¡¤Ë¤è¤Ã¤Æ¡¢¤½¤Î¦Ë¼°¤¬É½¸½¤¹¤ë´Ø¿ô¤¬¡¢Ìµ°ú¿ô¤«¡¢°ì°ú¿ô¤«¡¢Æó°ú¿ô¤«¡¢»°°ú¿ô¤«¤¬·è¤Þ¤ë¡£ ºÇ¤âÂç¤¤¤¥¤¥ó¥Ç¥Ã¥¯¥¹¤Î¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼¤¬·èÄê¤¹¤ë¡£ Îã¤¨¤Ð¡¢¼¡¤Î¤è¤¦¤Ë¤Ê¤ë¡£

    _1 + 5              // unary
    _1 * _1 + _1        // unary
    _1 + _2             // binary
    bind(f, _1, _2, _3) // 3-ary
    _3 + 10             // 3-ary

Ãí°Õ¤¬É¬Í×¤Ê¤Î¤Ï¡¢ºÇ¸å¤Î¹Ô¤Î¦Ë¼°¤Ï»°°ú¿ô¤Î´Ø¿ô¤òÀ¸À®¤¹¤ë¤³¤È¤Ç¤¢¤ë¡£ ¤½¤Î´Ø¿ô¤Ï¡¢Âè°ì¡¢ÂèÆó°ú¿ô¤òÌµ»ë¤·¡¢Âè»°°ú¿ô¤Ë 10 ¤ò²Ã¤¨¤ë¡£ ¤µ¤é¤Ë¡¢¦Ë¥Õ¥¡¥ó¥¯¥¿¤ÏºÇÄã¸Â¤Î°ú¿ô¤·¤«¤â¤¿¤Ê¤¤¡£ ¤·¤«¤·¡¢¤è¤êÂ¿¤¯¤Î°ú¿ô¤ò(¥µ¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤ë¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼¤Î¿ô¤Þ¤Ç¤Ç¤¢¤ì¤Ð)Í¿¤¨¤ë¤³¤È¤¬²ÄÇ½¤Ç¤¢¤ë¡£¤³¤Î¤³¤È¤Ï¼ÂºÝ¤ËÉ¬Í×¤È¤Ê¤ë¡£ Í¾¾ê¤Ê°ú¿ô¤ÏÃ±¤ËÌµ»ë¤µ¤ì¤ë¤À¤±¤Ç¤¢¤ë¡£ Îã¤¨¤Ð¡¢

    int i, j, k;
    _1(i, j, k)        // returns i, discards j and k
    (_2 + _2)(i, j, k) // returns j+j, discards i and k

¤È¤Ê¤ë¡£ ¤³¤Îµ¡Ç½¤ÎÇØ·Ê¤Ë¤¢¤ëÀß·×ÍýÍ³¤Ë¤Ë¤Ä¤¤¤Æ¤Ï¡¢Section 1¤ò¸«¤è¡£

The use of placeholders in the lambda expression determines whether the resulting function is nullary, unary, binary or 3-ary. The highest placeholder index is decisive. For example:

    _1 + 5              // unary
    _1 * _1 + _1        // unary
    _1 + _2             // binary
    bind(f, _1, _2, _3) // 3-ary
    _3 + 10             // 3-ary

Note that the last line creates a 3-ary function, which adds `10` to its third argument. The first two arguments are discarded. Furthermore, lambda functors only have a minimum arity. One can always provide more arguments (up the number of supported placeholders) that is really needed. The remaining arguments are just discarded. For example:

    int i, j, k;
    _1(i, j, k)        // returns i, discards j and k
    (_2 + _2)(i, j, k) // returns j+j, discards i and k

See Section 1 for the design rationale behind this functionality.

¤³¤ì¤é»°¤Ä¤Î¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼·¿¤Ë²Ã¤¨¤Æ¡¢»Í¤ÄÌÜ¤Î¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼·¿ `placeholderE_type` ¤â¤¢¤ë¡£ ¤³¤Î¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼¤Î»È¤¤Êý¤Ï¡¢¦Ë¼°Æâ¤Ç¤ÎÎã³°½èÍý¤òÀâÌÀ¤¹¤ë Section 5.7 ¤ÇÄêµÁ¤·¤Æ¤¤¤ë¡£

In addition to these three placeholder types, there is also a fourth placeholder type `placeholderE_type`. The use of this placeholder is defined in Section 5.7 describing exception handling in lambda expressions.

¼Â°ú¿ô¤¬¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼¤ËÅÏ¤µ¤ì¤¿¤È¤¡¢¥Ñ¥é¥á¥¿¤Ï¤¤¤Ä¤â»²¾È¤Ë¤è¤Ã¤ÆÅÏ¤µ¤ì¤ë¡£ ¤³¤ì¤Ï¡¢¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼¤ËÂÐ¤¹¤ë¤¤¤«¤Ê¤ëÉûºîÍÑ¤â¼Â°ú¿ô¤ËÈ¿±Ç¤µ¤»¤ë¤¿¤á¤Ç¤¢¤ë¡£ Îã¤¨¤Ð¡¢¼¡¤Î¤è¤¦¤Ë¤Ê¤ë¡£

    int i = 1;
    (_1 += 2)(i);         // i is now 3
    (++_1, cout << _1)(i) // i is now 4, outputs 4

When an actual argument is supplied for a placeholder, the parameter passing mode is always by reference. This means that any side-effects to the placeholder are reflected to the actual argument. For example:

    int i = 1;
    (_1 += 2)(i);         // i is now 3
    (++_1, cout << _1)(i) // i is now 4, outputs 4

### 5.2. Operator expressions

´ðËÜÅª¤Êµ¬Â§¤Ç¤Ï¡¢¾¯¤Ê¤¯¤È¤â°ì¤Ä¤Î¦Ë¼°¤ò°ú¿ô¤È¤¹¤ë¤¹¤Ù¤Æ¤Î C++ ¤Î±é»»»Ò¤Î¸Æ½Ð¤·¤Ï¡¢¤½¤ì¼«¿È¤¬¦Ë¼°¤Ç¤¢¤ë¡£ ¥ª¡¼¥Ð¡¼¥í¡¼¥É²ÄÇ½¤Ê±é»»»Ò¤Î¤Û¤È¤ó¤É¤Ï¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤ë¡£ Îã¤¨¤Ð¡¢°Ê²¼¤Î¤â¤Î¤ÏÍ¸ú¤Ê¦Ë¼°¤Ç¤¢¤ë¡£

    cout << _1, _2[_3] = _1 && false

The basic rule is that any C++ operator invocation with at least one argument being a lambda expression is itself a lambda expression. Almost all overloadable operators are supported. For example, the following is a valid lambda expression:

    cout << _1, _2[_3] = _1 && false

¤·¤«¤·¡¢¤¤¤¯¤Ä¤«¤ÎÀ©¸Â¤â¤¢¤ë¡£¤½¤ì¤é¤Ï¡¢C++ ¤Î±é»»»Ò¤Î¥ª¡¼¥Ð¡¼¥í¡¼¥É¤Îµ¬Â§¤Ë´ð¤Å¤¯¤â¤Î¤È¡¢ÆÃÊÌ¤Ê¾ì¹ç¤Î¤â¤Î¤È¤¢¤ë¡£

However, there are some restrictions that originate from the C++ operator overloading rules, and some special cases.

#### 5.2.1. Operators that cannot be overloaded

¤¤¤¯¤Ä¤«¤Î±é»»»Ò¤Ï´°Á´¤Ë¥ª¡¼¥Ð¡¼¥í¡¼¥É¤¹¤ë¤³¤È¤¬¤Ç¤¤Ê¤¤¡£(Îã¤¨¤Ð¡¢`::`, `.`, `.*`) ¤Þ¤¿¡¢¤¤¤¯¤Ä¤«¤Î±é»»»Ò¤Ë´Ø¤·¤Æ¤Ï¡¢ÊÖ¤êÃÍ¤Î·¿¤ÎÇ§¼±¤¬É¬Í×¤Ê¤¿¤á¤Ë¡¢¦Ë¼°¤òÀ¸À®¤¹¤ë¤¿¤á¤Î¥ª¡¼¥Ð¡¼¥í¡¼¥É¤ò¤¹¤ë¤³¤È¤¬¤Ç¤¤Ê¤¤¡£`->.`, `->`, `new`, `new[]`, `delete`, `delete[]` and `?:`(¾ò·ï±é»»»Ò)¤¬¤½¤¦¤¤¤Ã¤¿±é»»»Ò¤Ç¤¢¤ë¡£

Some operators cannot be overloaded at all (`::`, `.`, `.*`). For some operators, the requirements on return types prevent them to be overloaded to create lambda functors. These operators are `->.`, `->`, `new`, `new[]`, `delete`, `delete[]` and `?:` (the conditional operator).

#### 5.2.2. Assignment and subscript operators

¤³¤¦¤¤¤Ã¤¿±é»»»Ò¤Ï¥¯¥é¥¹¤Î¥á¥ó¥Ð´Ø¿ô¤È¤·¤Æ¼ÂÁõ¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£ ¤è¤Ã¤Æ¡¢±é»»»Ò¤Îº¸Â¦¤Î°ú¿ô¤Ï¦Ë¼°¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£ Îã¤¨¤Ð¼¡¤Î¤è¤¦¤Ë¤Ê¤ë¡£

    int i;
    _1 = i;      // ok
    i = _1;      // not ok. i is not a lambda expression

¤³¤ÎÀ©¸Â¤Ë´Ø¤¹¤ëÃ±½ã¤Ê²ò·èºö¤òSection 5.5¤Ë¤ª¤¤¤ÆÀâÌÀ¤¹¤ë¡£ ´Ê·é¤Ë¤¤¤¦¤È¡¢ º¸Â¦¤Î¥ª¥Ú¥é¥ó¥É¤ò ¼¡¤Î¤è¤¦¤ËÆÃÊÌ¤Ê´Ø¿ô `var` ¤Ç¥é¥Ã¥×¤·¤ÆÌÀ¼¨Åª¤Ë¦Ë¥Õ¥¡¥ó¥¯¥¿¤È¤¹¤ì¤Ð¤è¤¤¡£

    var(i) = _1; // ok

These operators must be implemented as class members. Consequently, the left operand must be a lambda expression. For example:

    int i;
    _1 = i;      // ok
    i = _1;      // not ok. i is not a lambda expression

There is a simple solution around this limitation, described in Section 5.5. In short, the left hand argument can be explicitly turned into a lambda functor by wrapping it with a special `var` function:

    var(i) = _1; // ok

#### 5.2.3. Logical operators

ÏÀÍý±é»»»Ò¤ÏÃ»ÍíÉ¾²Á¤Îµ¬Â§¤Ë½¾¤¦¡£ Îã¤¨¤Ð¡¢¼¡¤Î¥³¡¼¥É¤Ç¤Ï¡¢`i`¤Ï·è¤·¤Æ¥¤¥ó¥¯¥ê¥á¥ó¥È¤µ¤ì¤ë¤³¤È¤Ï¤Ê¤¤¡£

    bool flag = true; int i = 0;
    (_1 || ++_2)(flag, i);

Logical operators obey the short-circuiting evaluation rules. For example, in the following code, `i` is never incremented:

    bool flag = true; int i = 0;
    (_1 || ++_2)(flag, i);

#### 5.2.4. Comma operator

¥³¥ó¥Þ±é»»»Ò¤Ï¦Ë¼°Ãæ¤ÎÊ¸¤Î¶èÀÚ¤ê¤Ç¤¢¤ë¡£ ¥³¥ó¥Þ¤Ï´Ø¿ô¸Æ½Ð¤·¤Î°ú¿ô¤Î¶èÀÚ¤ê¤Ç¤â¤¢¤ë¤¿¤á¡¢Í¾Ê¬¤Ê³ç¸Ì¤¬É¬Í×¤Ë¤Ê¤ë¤³¤È¤¬¤¢¤ë¡£ Îã¤¨¤Ð¡¢¼¡¤Î¤è¤¦¤Ê¤È¤¤Ç¤¢¤ë¡£

    for_each(a.begin(), a.end(), (++_1, cout << _1));

`++_1, cout << _1` ¤ò°Ï¤¦Í¾Ê¬¤Ê³ç¸Ì¤¬¤Ê¤¤¤È¡¢¤³¤Î¥³¡¼¥É¤Ï `for_each` ¤ò 4 ¤Ä¤Î°ú¿ô¤Ç¸Æ½Ð¤½¤¦¤È¤·¤Æ¤¤¤ë¤È²ò¼á¤µ¤ì¤Æ¤·¤Þ¤¦¡£

Comma operator is the ‘statement separator’ in lambda expressions. Since comma is also the separator between arguments in a function call, extra parenthesis are sometimes needed:

    for_each(a.begin(), a.end(), (++_1, cout << _1));

Without the extra parenthesis around `++_1, cout << _1`, the code would be interpreted as an attempt to call `for_each` with four arguments.

¥³¥ó¥Þ±é»»»Ò¤òÍÑ¤¤¤ÆºîÀ®¤µ¤ì¤¿¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ï¡¢º¸¤Î¥ª¥Ú¥é¥ó¥É¤òÉ¾²Á¤·¤Æ¤«¤é¡¢±¦¤Î¥ª¥Ú¥é¥ó¥É¤òÉ¾²Á¤¹¤ë¤È¤¤¤¦ C++ ¤Îµ¬Â§¤ÈÆ±¤¸¤è¤¦¤ËÉ¾²Á¤¹¤ë¡£ ¾å¤Ëµó¤²¤¿Îã¤Ç¤Ï¡¢`a` ¤ÎÍ×ÁÇ¤½¤ì¤¾¤ì¤ËÂÐ¤·¤Æ¡¢¤Þ¤º¥¤¥ó¥¯¥ê¥á¥ó¥È¤µ¤ì¡¢¤½¤Î¸å¤Ç¥¹¥È¥ê¡¼¥à¤Ø½ñ¤½Ð¤µ¤ì¤ë¡£

The lambda functor created by the comma operator adheres to the C++ rule of always evaluating the left operand before the right one. In the above example, each element of `a` is first incremented, then written to the stream.

#### 5.2.5. Function call operator

´Ø¿ô¸Æ½Ð¤·±é»»»Ò¤Ë¤Ï¦Ë¥Õ¥¡¥ó¥¯¥¿¤òÉ¾²Á¤¹¤ë¸ú²Ì¤¬¤¢¤ë¡£ ¾¯¤Ê¤¤°ú¿ô¤Ç¤Î¸Æ½Ð¤·¤Ï¥³¥ó¥Ñ¥¤¥ë»þ¥¨¥é¡¼¤È¤·¤Æ¸¡½Ð¤µ¤ì¤ë¡£

The function call operators have the effect of evaluating the lambda functor. Calls with too few arguments lead to a compile time error.

#### 5.2.6. Member pointer operator

¥á¥ó¥Ð¥Ý¥¤¥ó¥¿±é»»»Ò `operator->*` ¤Ï¼«Í³¤Ë¥ª¡¼¥Ð¡¼¥í¡¼¥É²ÄÇ½¤Ç¤¢¤ë¡£ ¤è¤Ã¤Æ¡¢¥æ¡¼¥¶ÄêµÁ·¿¤Ë´Ø¤·¤Æ¤Ï¥á¥ó¥Ð¥Ý¥¤¥ó¥¿±é»»»Ò¤ÎÆÃÊÌ¤Ê¾ì¹ç¤Ï¤Ê¤¤¡£ ¤·¤«¤·¡¢ÁÈ¤ß¹þ¤ß·¿¤Ë¤ª¤¤¤Æ¤Ï¤â¤¦¾¯¤·Ê£»¨¤Ê¾ì¹ç¤¬¤¢¤ë¡£ ÁÈ¤ß¹þ¤ß¤Î¥á¥ó¥Ð¥Ý¥¤¥ó¥¿±é»»»Ò¤Ï¡¢º¸¤Î°ú¿ô¤¬¤¢¤ë¥¯¥é¥¹ `A` ¤Î¥ª¥Ö¥¸¥§¥¯¥È¤ò»Ø¤¹¥Ý¥¤¥ó¥¿¤Ç¤¢¤ê¡¢±¦Â¦¤Î°ú¿ô¤¬ `A` ¤Î¥á¥ó¥Ð¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¤«¡¢`A` ¤«¤éÇÉÀ¸¤·¤¿¥¯¥é¥¹¤Î¥á¥ó¥Ð¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤Ã¤¿¾ì¹ç¤ËÅ¬ÍÑ¤µ¤ì¤ë¡£ ¤µ¤é¤Ë¡¢¼¡¤ÎÆó¤Ä¤Î¾ì¹ç¤ÏÊ¬¤±¤Æ¹Í¤¨¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
The member pointer operator `operator->*` can be overloaded freely. Hence, for user defined types, member pointer operator is no special case. The built-in meaning, however, is a somewhat more complicated case. The built-in member pointer operator is applied if the left argument is a pointer to an object of some class `A`, and the right hand argument is a pointer to a member of `A`, or a pointer to a member of a class from which `A` derives. We must separate two cases:

- ¤Þ¤º¡¢±¦Â¦¤Î°ú¿ô¤¬¥á¥ó¥ÐÊÑ¿ô¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¤È¤¤Ç¤¢¤ë¡£ ¤³¤Î¾ì¹ç¤Ï¡¢¦Ë¥Õ¥¡¥ó¥¯¥¿¤ÏÃ±½ã¤Ë¼Â°ú¿ô¤ÎÂåÆþ¤ò¹Ô¤¤¡¢°ú¿ô¤Î¥Ý¥¤¥ó¥¿¤¬»Ø¤·¤Æ¤¤¤ë¥á¥ó¥ÐÊÑ¿ô¤Î»²¾È¤òÊÖ¤¹ÁÈ¤ß¹þ¤ß¤Î¥á¥ó¥Ð¥Ý¥¤¥ó¥¿±é»»»Ò¤¬¸Æ¤Ð¤ì¤ë¡£ Îã¤¨¤Ð¡¢¼¡¤Î¤è¤¦¤Ë¤Ê¤ë¡£

      struct A { int d; };
      A* a = new A();
        ...
      (a ->* &A::d);     // returns a reference to a->d
      (_1 ->* &A::d)(a); // likewise

- ¼¡¤Ë¡¢±¦Â¦¤Î°ú¿ô¤¬¥á¥ó¥Ð´Ø¿ô¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¤È¤¤Ç¤¢¤ë¡£ ¤³¤Î¤è¤¦¤ÊÁÈ¤ß¹þ¤ß¤Î±é»»»Ò¤Î¸Æ½Ð¤·¤Ï¡¢°ì¼ï¤Î¥á¥ó¥Ð´Ø¿ô¤ÎÃÙ±ä¸Æ½Ð¤·¤Î¤è¤¦¤Ê·ë²Ì¤È¤Ê¤ë¡£ ¤³¤Î¤è¤¦¤Ê¼°¤Î¸å¤Ë¤Ï¡¢¥á¥ó¥Ð´Ø¿ô¤ÎÃÙ±ä¸Æ½Ð¤·¤Î¤¿¤á¤Î°ú¿ô¥ê¥¹¥È¤¬Â³¤«¤Ê¤¯¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£ Îã¤¨¤Ð¡¢¼¡¤Î¤è¤¦¤Ë¤Ê¤ë¡£

      struct B { int foo(int); };
      B* b = new B();
        ...
      (b ->* &B::foo)         // returns a delayed call to b->foo
                              // a function argument list must follow
      (b ->* &B::foo)(1)      // ok, calls b->foo(1)

      (_1 ->* &B::foo)(b);    // returns a delayed call to b->foo,
                              // no effect as such
      (_1 ->* &B::foo)(b)(1); // calls b->foo(1)

- The right hand argument is a pointer to a data member. In this case the lambda functor simply performs the argument substitution and calls the built-in member pointer operator, which returns a reference to the member pointed to. For example:

      struct A { int d; };
      A* a = new A();
        ...
      (a ->* &A::d);     // returns a reference to a->d
      (_1 ->* &A::d)(a); // likewise

- The right hand argument is a pointer to a member function. For a built-in call like this, the result is kind of a delayed member function call. Such an expression must be followed by a function argument list, with which the delayed member function call is performed. For example:

      struct B { int foo(int); };
      B* b = new B();
        ...
      (b ->* &B::foo)         // returns a delayed call to b->foo
                              // a function argument list must follow
      (b ->* &B::foo)(1)      // ok, calls b->foo(1)

      (_1 ->* &B::foo)(b);    // returns a delayed call to b->foo,
                              // no effect as such
      (_1 ->* &B::foo)(b)(1); // calls b->foo(1)

### 5.3. Bind expressions

bind ¼°¤Ë¤Ï¼¡¤ÎÆó¤Ä¤Î·Á¤¬¤¢¤ë¡£

    bind(target-function, bind-argument-list)
    bind(target-member-function, object-argument, bind-argument-list)

bind ¼°¤Ï´Ø¿ô¤Î¸Æ½Ð¤·¤òÃÙ±ä¤µ¤»¤ë¡£ ¤â¤·¡¢target-function ¤¬ n°ú¿ô¤Î´Ø¿ô¤Ç¤¢¤ì¤Ð¡¢`bind-argument-list` ¤âÆ±ÍÍ¤Ë n ¸Ä¤Î°ú¿ô¤ò´Þ¤ó¤Ç¤¤¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£ ¸½ºß¤Î¥Ð¡¼¥¸¥ç¥ó¤Î BLL ¤Ç¤Ï¡¢°ú¿ô¤Î¿ô n ¤Ë¤Ï¡¢0 <= n <= 9 ¤È¤¤¤¦À©¸Â¤¬¤¢¤ë¡£ ¥á¥ó¥Ð´Ø¿ô¤ËÅ¬ÍÑ¤¹¤ë¾ì¹ç¤Ë¤Ï¡¢ÂÐ¾Ý¤Î¥ª¥Ö¥¸¥§¥¯¥È¤¬°ú¿ô¤È¤·¤ÆÅÏ¤µ¤ì¤ë¤¿¤á¡¢°ú¿ô¤Î¿ô¤ÏºÇÂç 8 ¸Ä¤ËÍÞ¤¨¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£ ´ðËÜÅª¤Ë¤Ï¡¢°ú¿ô¤¬¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼¤Ç¤¢¤ë¡¢¤Þ¤¿¤è¤ê°ìÈÌÅª¤Ë¸À¤¨¤Ð¡¢¦Ë¼°¤Ç¤¢¤ë¤³¤È¤ò½ü¤±¤Ð¡¢`bind-argument-list` ¤ÏÌÜÅª¤Î´Ø¿ô¤Ø¤ÎÍ¸ú¤Ê°ú¿ô¤Î¥ê¥¹¥È¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£ ¤Þ¤¿¡¢ÌÜÅª¤Î´Ø¿ô¤Ï¦Ë¼°¤Ç¤¢¤Ã¤Æ¤â¤è¤¤¡£ bind ¼°¤Î·ë²Ì¤Ï¡¢Ìµ°ú¿ô¡¢°ì°ú¿ô¡¢Æó°ú¿ô¡¢¤Þ¤¿¤Ï¡¢»°°ú¿ô¤Î´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤Ç¤¢¤ë¡£ ¤³¤Î°ú¿ô¤Î¿ô¤Ï¡¢`bind-argument-list` Æâ¤Î¥×¥ì¡¼¥¹¥Û¥ë¥À¡¼¤Î»È¤¤Êý¤Ë¤è¤Ã¤Æ·èÄê¤µ¤ì¤ë¡£(¾Ü¤·¤¯¤Ï Section 5.1 ¤ò¸«¤è)

Bind expressions can have two forms:

    bind(target-function, bind-argument-list)
    bind(target-member-function, object-argument, bind-argument-list)

A bind expression delays the call of a function. If this target function is n-ary, then the `bind-argument-list` must contain n arguments as well. In the current version of the BLL, 0 <= n <= 9 must hold. For member functions, the number of arguments must be at most 8, as the object argument takes one argument position. Basically, the `bind-argument-list` must be a valid argument list for the target function, except that any argument can be replaced with a placeholder, or more generally, with a lambda expression. Note that also the target function can be a lambda expression. The result of a bind expression is either a nullary, unary, binary or 3-ary function object depending on the use of placeholders in the `bind-argument-list` (see Section 5.1).

¼¡¤ÎÎã¤Ç¼¨¤¹¤è¤¦¤Ë¡¢bind ¼°¤ÇºîÀ®¤µ¤ì¤¿¦Ë¥Õ¥¡¥ó¥¯¥¿¤ÎÊÖ¤êÃÍ¤Ï¡¢¥Æ¥ó¥×¥ì¡¼¥È°ú¿ô¤È¤·¤ÆÌÀ¼¨Åª¤Ë»ØÄê¤µ¤ì¤ë¡£

    bind<RET>(target-function, bind-argument-list)

¤³¤ÎÌÀ¼¨Åª¤Ê»ØÄê¤Ï¡¢ÌÜÅª¤Î´Ø¿ô¤ÎÊÖ¤êÃÍ¤Î·¿¤ò¿äÏÀ¤Ç¤¤Ê¤«¤Ã¤¿¾ì¹ç¤Î¤ßÉ¬Í×¤È¤Ê¤ë¡£

The return type of the lambda functor created by the bind expression can be given as an explicitly specified template parameter, as in the following example:

    bind<RET>(target-function, bind-argument-list)

This is only necessary if the return type of the target function cannot be deduced.

°Ê²¼¤Î¥µ¥Ö¥»¥¯¥·¥ç¥ó¤Ç¤Ï¡¢ÍÍ¡¹¤Ê bind ¼°¤Î·Á¤Ë¤Ä¤¤¤ÆÀâÌÀ¤·¤Æ¤¤¤ë¡£

The following sections describe the different types of bind expressions.

#### 5.3.1. Function pointers or references as targets

ÌÜÅª¤Î´Ø¿ô¤Ï¡¢´Ø¿ô¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤Ã¤Æ¤â»²¾È¤Ç¤¢¤Ã¤Æ¤â¤è¤¯¡¢¤Þ¤¿¡¢¤½¤Î´Ø¿ô¤¬Â«Çû¤µ¤ì¤Æ¤â¡¢Â«Çû¤µ¤ì¤Æ¤Ê¤¯¤È¤â¤è¤¤¡£ Îã¤¨¤Ð¡¢¼¡¤ÎÎã¤Î bind ¼°¤ÏÁ´¤ÆÍ¸ú¤Ç¤¢¤ë¡£

    X foo(A, B, C); A a; B b; C c;
    bind(foo, _1, _2, c)(a, b);
    bind(&foo, _1, _2, c)(a, b);
    bind(_1, a, b, c)(foo);

ÊÖ¤êÃÍ¤Î·¿¤Î¿äÏÀ¤Ï¤³¤ì¤é¤Î bind ¼°¤Ç¤ÏÁ´¤Æ²ò·è¤Ç¤¤ë¡£

The target function can be a pointer or a reference to a function and it can be either bound or unbound. For example:

    X foo(A, B, C); A a; B b; C c;
    bind(foo, _1, _2, c)(a, b);
    bind(&foo, _1, _2, c)(a, b);
    bind(_1, a, b, c)(foo);

The return type deduction always succeeds with this type of bind expressions.

C++ ¤Ç¤Ï¥ª¡¼¥Ð¡¼¥í¡¼¥É¤µ¤ì¤¿´Ø¿ô¤Î¥¢¥É¥ì¥¹¤Ï¡¢Ä¾ÀÜ¥¢¥É¥ì¥¹¤¬»ØÄê¤µ¤ì¤ë¤«¡¢ Û£Ëæ¤µ¤ò²ò·è¤¹¤ë·¿¤ò»ý¤ÄÊÑ¿ô¤Î½é´ü²½¤È¤·¤Æ»ÈÍÑ¤µ¤ì¤ë¤«¡¢ ÌÀ¼¨Åª¤Ê¥¥ã¥¹¥È¤¬»ÈÍÑ¤µ¤ì¤¿¾ì¹ç¤Î¤ß¼èÆÀ¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£ ¤Ä¤Þ¤ê¡¢¥ª¡¼¥Ð¡¼¥í¡¼¥É¤µ¤ì¤¿´Ø¿ô¤Ï¡¢¼¡¤Ë¼¨¤¹¤è¤¦¤ËÄ¾ÀÜ bind ¼°¤Ç»ÈÍÑ¤¹¤ë¤³¤È¤Ï¤Ç¤¤Ê¤¤¡£

    void foo(int);
    void foo(float);
    int i;
      ...
    bind(&foo, _1)(i);                            // error
      ...
    void (*pf1)(int) = &foo;
    bind(pf1, _1)(i);                             // ok
    bind(static_cast<void(*)(int)>(&foo), _1)(i); // ok

Note, that in C++ it is possible to take the address of an overloaded function only if the address is assigned to, or used as an initializer of, a variable, the type of which solves the amibiguity, or if an explicit cast expression is used. This means that overloaded functions cannot be used in bind expressions directly, e.g.:

    void foo(int);
    void foo(float);
    int i;
      ...
    bind(&foo, _1)(i);                            // error
      ...
    void (*pf1)(int) = &foo;
    bind(pf1, _1)(i);                             // ok
    bind(static_cast<void(*)(int)>(&foo), _1)(i); // ok

#### 5.3.2. Member functions as targets

bind ¼°¤Ë¤ª¤¤¤Æ¥á¥ó¥Ð´Ø¿ô¤Ø¤Î¥Ý¥¤¥ó¥¿¤ò»ÈÍÑ¤¹¤ë¤¿¤á¤ÎÊ¸Ë¡¤Ï¼¡¤ÎÄÌ¤ê¤Ç¤¢¤ë¡£ The syntax for using pointers to member function in bind expression is:

    bind(target-member-function, object-argument, bind-argument-list)

¥ª¥Ö¥¸¥§¥¯¥È¤Ï¡¢»²¾È¤Ç¤â¥Ý¥¤¥ó¥¿¤Ç¤â¤è¤¤¡£BLL ¤ÏÎ¾Êý¤Î¾ì¹ç¤òÆ±°ì¤Î¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤Ç¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤ë¡£

    bool A::foo(int) const;
    A a;
    vector<int> ints;
      ...
    find_if(ints.begin(), ints.end(), bind(&A::foo, a, _1));
    find_if(ints.begin(), ints.end(), bind(&A::foo, &a, _1));

The syntax for using pointers to member function in bind expression is:

    bind(target-member-function, object-argument, bind-argument-list)

The object argument can be a reference or pointer to the object, the BLL supports both cases with a uniform interface:

    bool A::foo(int) const;
    A a;
    vector<int> ints;
      ...
    find_if(ints.begin(), ints.end(), bind(&A::foo, a, _1));
    find_if(ints.begin(), ints.end(), bind(&A::foo, &a, _1));

Æ±ÍÍ¤Ë¡¢¥ª¥Ö¥¸¥§¥¯¥È¤¬Â«Çû¤µ¤ì¤Æ¤¤¤Ê¤±¤ì¤Ð¡¢bind ¼°¤Î·ë²Ì¤Î¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ï¼ÂºÝ¤ËÅÏ¤µ¤ì¤¿°ú¿ô¤¬¥Ý¥¤¥ó¥¿¤«»²¾È¤«¤Ë¤è¤Ã¤Æ¤É¤Á¤é¤â¸Æ¤Ö¤³¤È¤¬¤Ç¤¤ë¡£¼¡¤Ë¤½¤ÎÎã¤ò¼¨¤¹¡£

    bool A::foo(int);
    list<A> refs;
    list<A*> pointers;
      ...
    find_if(refs.begin(), refs.end(), bind(&A::foo, _1, 1));
    find_if(pointers.begin(), pointers.end(), bind(&A::foo, _1, 1));

Similarly, if the object argument is unbound, the resulting lambda functor can be called both via a pointer or a reference:

    bool A::foo(int);
    list<A> refs;
    list<A*> pointers;
      ...
    find_if(refs.begin(), refs.end(), bind(&A::foo, _1, 1));
    find_if(pointers.begin(), pointers.end(), bind(&A::foo, _1, 1));

¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤ÏÆ±¤¸¤Ç¤¢¤Ã¤Æ¤â¡¢¥ª¥Ö¥¸¥§¥¯¥È¤¬¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¤«¡¢»²¾È¤Ç¤¢¤ë¤«¤Ë¤Ï¡¢½ÅÍ×¤Ê°ÕÌ£Åª¤Ê°ã¤¤¤¬¤¢¤ë¡£¤³¤Î°ã¤¤¤Ï¡¢´Ø¿ô `bind` ¤Î°ú¿ô¤Î¼è¤êÊý¤È¡¢Â«Çû¤µ¤ì¤¿°ú¿ô¤ò¦Ë¥Õ¥¡¥ó¥¯¥¿Æâ¤Ë³ÊÇ¼¤¹¤ëÊýË¡¤Ë¤è¤ë¤â¤Î¤Ç¤¢¤ë¡£ ¥ª¥Ö¥¸¥§¥¯¥È¤ÏÂ¾¤Î bind ¤Î°ú¿ô¤ÈÆ±ÍÍ¤ËÅÏ¤µ¤ì¡¢³ÊÇ¼¤µ¤ì¤ë¡£(Section 4.4 »²¾È); ¤Ä¤Þ¤ê¡¢const ¤Ê»²¾È¤È¤·¤ÆÅÏ¤µ¤ì¡¢const ¤Ê¥³¥Ô¡¼¤È¤·¤Æ¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ë³ÊÇ¼¤µ¤ì¤ë¡£ ¤³¤ì¤Ë¤è¤ê¡¢¦Ë¥Õ¥¡¥ó¥¯¥¿¤È¸µ¡¹¤Î¥á¥ó¥Ð´Ø¿ô¤ä¡¢¸«¤¿ÌÜ¤Ë¤ÏÆ±¤¸¦Ë¥Õ¥¡¥ó¥¯¥¿¤Î´Ö¤ËÈóÂÐ¾ÎÀ¤¬À¸¤¸¤Æ¤·¤Þ¤¦¡£Îã¤¨¤Ð¡¢¼¡¤Î¤è¤¦¤Ê¾ì¹ç¤Ç¤¢¤ë¡£

    class A {
      int i; mutable int j;
    public:

      A(int ii, int jj) : i(ii), j(jj) {};
      void set_i(int x) { i = x; };
      void set_j(int x) const { j = x; };
    };

Even though the interfaces are the same, there are important semantic differences between using a pointer or a reference as the object argument. The differences stem from the way `bind`-functions take their parameters, and how the bound parameters are stored within the lambda functor. The object argument has the same parameter passing and storing mechanism as any other bind argument slot (see Section 4.4); it is passed as a const reference and stored as a const copy in the lambda functor. This creates some asymmetry between the lambda functor and the original member function, and between seemingly similar lambda functors. For example:

    class A {
      int i; mutable int j;
    public:

      A(int ii, int jj) : i(ii), j(jj) {};
      void set_i(int x) { i = x; };
      void set_j(int x) const { j = x; };
    };

¥Ý¥¤¥ó¥¿¤È¤·¤Æ»ÈÍÑ¤µ¤ì¤¿¾ì¹ç¤Ë¤Ï¡¢¼¡¤ÎÆ°ºî¤Ï¶²¤é¤¯¥×¥í¥°¥é¥Þ¤¬´üÂÔ¤·¤¿¤â¤Î¤Ç¤¢¤ë¡£

    A a(0,0); int k = 1;
    bind(&A::set_i, &a, _1)(k); // a.i == 1
    bind(&A::set_j, &a, _1)(k); // a.j == 1

¥ª¥Ö¥¸¥§¥¯¥È¤Î const ¤Ê¥³¥Ô¡¼¤¬³ÊÇ¼¤µ¤ì¤Æ¤¤¤¿¤È¤·¤Æ¤â¡¢¸µ¤Î¥ª¥Ö¥¸¥§¥¯¥È `a`¤âÊÑ¹¹¤µ¤ì¤Æ¤¤¤ë¡£ ¤³¤ì¤Ï¡¢¥ª¥Ö¥¸¥§¥¯¥È¤¬¥Ý¥¤¥ó¥¿¤È¤·¤ÆÅÏ¤µ¤ì¤¿¤¿¤á¤Ç¤¢¤ê¡¢¥Ý¥¤¥ó¥¿¤¬»Ø¤¹¥ª¥Ö¥¸¥§¥¯¥È¤¬¥³¥Ô¡¼¤µ¤ì¤¿¤Î¤Ç¤Ï¤Ê¤¯¡¢¥Ý¥¤¥ó¥¿¼«¿È¤¬¥³¥Ô¡¼¤µ¤ì¤¿¤¿¤á¤Ç¤¢¤ë¡£ »²¾È¤È¤·¤Æ»ÈÍÑ¤µ¤ì¤¿¾ì¹ç¤Ë¤Ï¡¢°Û¤Ã¤¿Æ°ºî¤È¤Ê¤ë¡£

    A a(0,0); int k = 1;
    bind(&A::set_i, a, _1)(k); // error; a const copy of a is stored.
                               // Cannot call a non-const function set_i
    bind(&A::set_j, a, _1)(k); // a.j == 0, as a copy of a is modified

When a pointer is used, the behavior is what the programmer might expect:

    A a(0,0); int k = 1;
    bind(&A::set_i, &a, _1)(k); // a.i == 1
    bind(&A::set_j, &a, _1)(k); // a.j == 1

Even though a const copy of the object argument is stored, the original object `a` is still modified. This is since the object argument is a pointer, and the pointer is copied, not the object it points to. When we use a reference, the behaviour is different:

    A a(0,0); int k = 1;
    bind(&A::set_i, a, _1)(k); // error; a const copy of a is stored.
                               // Cannot call a non-const function set_i
    bind(&A::set_j, a, _1)(k); // a.j == 0, as a copy of a is modified

¥³¥Ô¡¼¤ÎÈ¯À¸¤òËÉ¤°¤¿¤á¤Ë¤Ï¡¢¼¡¤Î¤è¤¦¤Ë `ref` ¤ä `cref` ¤Î¥é¥Ã¥Ñ¤ò»ÈÍÑ¤¹¤ì¤Ð¤è¤¤¡£(`var` ¤ä `constant_ref` ¤âÆ±ÍÍ¤Ç¤¢¤ë )

    bind(&A::set_i, ref(a), _1)(k); // a.j == 1
    bind(&A::set_j, cref(a), _1)(k); // a.j == 1

To prevent the copying from taking place, one can use the `ref` or `cref` wrappers (`var` and `constant_ref` would do as well):

    bind(&A::set_i, ref(a), _1)(k); // a.j == 1
    bind(&A::set_j, cref(a), _1)(k); // a.j == 1

¤³¤³¤Þ¤Ç¤Î»ö¹à¤ÎÆâÍÆ¤ÏÂ«Çû¤µ¤ì¤¿°ú¿ô¤Ë¤Î¤ß´Ø·¸¤¹¤ë¡£ ¥ª¥Ö¥¸¥§¥¯¥È¤¬Â«Çû¤µ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ë¤Ï¡¢°ú¿ô¤Ï¾ï¤Ë»²¾È¤È¤·¤ÆÅÏ¤µ¤ì¤ë¡£ ¤è¤Ã¤Æ¡¢¼¡¤ÎÆó¤Ä¤Î¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ç¤Ï¡¢°ú¿ô `a` ¤Ï¥³¥Ô¡¼¤µ¤ì¤Ê¤¤¡£

    A a(0,0);
    bind(&A::set_i, _1, 1)(a); // a.i == 1
    bind(&A::set_j, _1, 1)(a); // a.j == 1

Note that the preceding discussion is relevant only for bound arguments. If the object argument is unbound, the parameter passing mode is always by reference. Hence, the argument `a` is not copied in the calls to the two lambda functors below:

    A a(0,0);
    bind(&A::set_i, _1, 1)(a); // a.i == 1
    bind(&A::set_j, _1, 1)(a); // a.j == 1

#### 5.3.3. Member variables as targets

¥á¥ó¥ÐÊÑ¿ô¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ï¼ÂºÝ¤Ë¤Ï´Ø¿ô¤Ç¤Ï¤Ê¤¤¡£ ¤·¤«¤·¡¢¤½¤ì¤Ë¤â´Ø¤é¤º¡¢´Ø¿ô `bind` ¤ÏÂè°ì°ú¿ô¤Ë¥á¥ó¥ÐÊÑ¿ô¤Ø¤Î¥Ý¥¤¥ó¥¿¤ò¼è¤ë¤³¤È¤¬²ÄÇ½¤Ç¤¢¤ë¡£ ¤³¤Î¤è¤¦¤Ê bind ¤ò¸Æ½Ð¤¹¤È¡¢¤½¤Î¥á¥ó¥ÐÊÑ¿ô¤Î»²¾È¤¬ÊÖ¤µ¤ì¤ë¡£ Îã¤¨¤Ð¡¢¼¡¤Î¤è¤¦¤Ë¤Ê¤ë¡£

    struct A { int data; };
    A a;
    bind(&A::data, _1)(a) = 1;     // a.data == 1

»²¾È¤µ¤ì¤ë¥á¥ó¥ÐÊÑ¿ô¤ò»ý¤Ä¥ª¥Ö¥¸¥§¥¯¥È¤Î const/volatile ¤Î½¤¾þ»Ò¤ÏÂº½Å¤µ¤ì¤ë¡£ Îã¤¨¤Ð¡¢¼¡¤ÎÎã¤Ç¤Ï const ¤Ê°ÌÃÖ¤Ø½ñ¤¹þ¤â¤¦¤È¤·¤Æ¤¤¤ë¡£ For example, the following tries to write into a const location:

    const A ca = a;
    bind(&A::data, _1)(ca) = 1;     // error

A pointer to a member variable is not really a function, but the first argument to the `bind` function can nevertheless be a pointer to a member variable. Invoking such a bind expression returns a reference to the data member. For example:

    struct A { int data; };
    A a;
    bind(&A::data, _1)(a) = 1;     // a.data == 1

The cv-qualifiers of the object whose member is accessed are respected. For example, the following tries to write into a const location:

    const A ca = a;
    bind(&A::data, _1)(ca) = 1;     // error

#### 5.3.4. Function objects as targets

´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤âÌÜÅª¤Î´Ø¿ô¤È¤·¤Æ»ÈÍÑ¤Ç¤¤ë¡£ ´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤È¤Ï¡¢´Ø¿ô¸Æ½Ð¤·±é»»»Ò¤¬ÄêµÁ¤µ¤ì¤¿¥¯¥é¥¹¥ª¥Ö¥¸¥§¥¯¥È¤Î¤³¤È¤Ç¤¢¤ë¡£ °ìÈÌÅª¤Ë¤Ï¡¢BLL ¤ÏÇ¤°Õ¤Î´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤ÎÊÖ¤êÃÍ¤Î·¿¤ò¿äÏÀ¤¹¤ë¤³¤È¤Ï¤Ç¤¤Ê¤¤¡£ ¤·¤«¤·¡¢¤³¤Îµ¡Ç½¤òÆÃÄê¤Î´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤Î¥¯¥é¥¹¤ËÄó¶¡¤¹¤ëÊýË¡¤¬¤¢¤ë¡£

Function objects, that is, class objects which have the function call operator defined, can be used as target functions. In general, BLL cannot deduce the return type of an arbitrary function object. However, there is a method for giving BLL this capability for a certain function object class.

##### The sig template

BLL ¤¬´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤ÎÊÖ¤êÃÍ¤Î·¿¤òÇ§¼±¤¹¤ë¤¿¤á¤Ë¤Ï¡¢ `type` ¤È¤¤¤¦ÊÖ¤êÃÍ¤Î·¿¤ò»ØÄê¤¹¤ë typedef ¤ò»ý¤Ã¤¿ `sig<Args>` ¥á¥ó¥Ð¥Æ¥ó¥×¥ì¡¼¥È¹½Â¤ÂÎ¤òÄó¶¡¤¹¤ëÉ¬Í×¤¬¤¢¤ë¡£ ¤³¤ì¤¬´ÊÃ±¤ÊÎã¤Ç¤¢¤ë¡£

    struct A {
      template <class Args> struct sig { typedef B type; }
      B operator()(X, Y, Z);
    };

To make BLL aware of the return type(s) of a function object one needs to provide a member template struct `sig<Args>` with a typedef `type` that specifies the return type. Here is a simple example:

    struct A {
      template <class Args> struct sig { typedef B type; }
      B operator()(X, Y, Z);
    };

¥Æ¥ó¥×¥ì¡¼¥È°ú¿ô `Args` ¤Ï `tuple`(¤è¤êÀµ³Î¤Ë¸À¤¨¤Ð¡¢`cons`¥ê¥¹¥È) ·¿ [tuple] ¤Ç¤¢¤ë¡£ ¤½¤·¤Æ¡¢ºÇ½é¤ÎÍ×ÁÇ¤Ï´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¼«¿È¤Ç¤¢¤ê¡¢»Ä¤ê¤ÎÍ×ÁÇ¤Ï´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤¬¸Æ¤Ð¤ì¤ë¤È¤¤Î°ú¿ô¤Î·¿¤Ç¤¢¤ë¡£ ¤³¤ì¤Ï¡¢`return_type` typedef ¤Ç»ØÄê¤¹¤ëÉ¸½à¥é¥¤¥Ö¥é¥ê¤Î´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤ÎÊÖ¤êÃÍ¤Î·¿¤ÎÄêµÁ¤Î»ÅÍÍ¤ÈÈæ³Ó¤¹¤ë¤È¡¢É¬Í×°Ê¾å¤ËÊ£»¨¤Ë¸«¤¨¤ë¤«¤â¤·¤ì¤Ê¤¤¡£ ¤·¤«¤·¡¢ÊÖ¤êÃÍ¤Î·¿¤òÃ±½ã¤Ê typedef ¤Ç¼¨¤¹¤¿¤á¤Ë¤Ï¡¢Æó¤Ä¤Î½ÅÍ×¤ÊÀ©¸Â¤¬É¬Í×¤È¤Ê¤ë¡£
The template argument `Args` is a `tuple` (or more precisely a `cons` list) type [tuple], where the first element is the function object type itself, and the remaining elements are the types of the arguments, with which the function object is being called. This may seem overly complex compared to the Standard Library convention for defining the return type of a function object with the `return_type` typedef. Howver, there are two significant restrictions with using just a simple typedef to express the return type:

1. ´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤¬Ê£¿ô¤Î´Ø¿ô¸Æ½Ð¤·±é»»»Ò¤òÄêµÁ¤·¤Æ¤¤¤¿¾ì¹ç¡¢¤½¤ì¤é¤ËÂÐ¤·°Û¤Ê¤ëÊÖ¤êÃÍ¤Î·¿¤ò»ØÄê¤¹¤ëÊýË¡¤¬¤Ê¤¤¡£
   If the function object defines several function call operators, there is no way to specify different result types for them.

2. ´Ø¿ô¸Æ½Ð¤·±é»»»Ò¤¬¥Æ¥ó¥×¥ì¡¼¥È¤Ç¤¢¤Ã¤¿¾ì¹ç¡¢ÊÖ¤êÃÍ¤Î·¿¤Ï¤½¤Î¥Æ¥ó¥×¥ì¡¼¥È°ú¿ô¤Ë°ÍÂ¸¤¹¤ë¤«¤â¤·¤ì¤Ê¤¤¡£ ¤è¤Ã¤Æ¡¢typedef ¤â¥Æ¥ó¥×¥ì¡¼¥È¤Ç¤¢¤ë¤Ï¤º¤Ç¤¢¤ë¡£¤·¤«¤·¡¢C++ ¤Ï¤³¤ì¤ò¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤Ê¤¤¡£
   If the function call operator is a template, the result type may depend on the template parameters. Hence, the typedef ought to be a template too, which the C++ language does not support.

°Ê²¼¤Î¥³¡¼¥É¤Ï¡¢ÊÖ¤êÃÍ¤Î·¿¤¬°ú¿ô¤Î°ì¤Ä¤Î·¿¤Ë°ÍÂ¸¤¹¤ëÎã¤Ç¤¢¤ê¡¢ `sig` ¥Æ¥ó¥×¥ì¡¼¥È¤Ë¤è¤Ã¤Æ¡¢¤É¤Î¤è¤¦¤Ë°ÍÂ¸´Ø·¸¤òÉ½¸½¤Ç¤¤ë¤«¤ò¼¨¤·¤Æ¤¤¤ë¡£

    struct A {

      // the return type equals the third argument type:
      template<class T1, T2, T3>
      T3 operator()(const T1& t1, const T2& t2, const T3& t3);

      template <class Args>
      class sig {
        // get the third argument type (4th element)
        typedef typename
          boost::tuples::element<3, Args>::type T3;
      public:
        typedef typename
          boost::remove_cv<T3>::type type;
      }
    };

The following code shows an example, where the return type depends on the type of one of the arguments, and how that dependency can be expressed with the `sig` template:

    struct A {

      // the return type equals the third argument type:
      template<class T1, T2, T3>
      T3 operator()(const T1& t1, const T2& t2, const T3& t3);

      template <class Args>
      class sig {
        // get the third argument type (4th element)
        typedef typename
          boost::tuples::element<3, Args>::type T3;
      public:
        typedef typename
          boost::remove_cv<T3>::type type;
      }
    };

`Args` ¥¿¥×¥ë¤ÎÍ×ÁÇ¤Ï¾ï¤Ë»²¾È¤Ç¤Ê¤¤·¿¤Ç¤¢¤ë¡£ ¤µ¤é¤Ë¡¢Í×ÁÇ¤Î·¿¤Ï¡¢const ¤ä volatile ¤Þ¤¿¤Ï¡¢¤½¤ÎÎ¾Êý¤Ë¤è¤Ã¤Æ½¤¾þ¤µ¤ì¤ë¤³¤È¤¬¤¢¤ë¡£ ¤³¤ì¤Ë¤è¤ê¡¢°ú¿ô¤Î const/volatile ½¤¾þ»Ò¤Ë¤è¤Ã¤Æ¡¢ÊÖ¤êÃÍ¤Î·¿¤Ø±Æ¶Á¤òÍ¿¤¨¤ë¤³¤È¤¬²ÄÇ½¤È¤Ê¤ë¡£ ¤Þ¤¿¡¢ÀøºßÅª¤Ë const/volatile ¤Ë¤è¤Ã¤Æ½¤¾þ»Ò¤µ¤ì¤¿´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤Î·¿¼«¿È¤¬ `Args` ¥¿¥×¥ë¤Ë´Þ¤Þ¤ì¤Æ¤¤¤ë¤³¤È¤Ë¤è¤ê¡¢´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¥¯¥é¥¹¤Ï¡¢const ¤È Èó const (¤Þ¤¿¤Ï¡¢volatile ¤ä const volatile )¤Î´Ø¿ô¸Æ½Ð¤·±é»»»Ò¤òÎ¾Êý´Þ¤à¤³¤È¤¬¤Ç¤¡¢¤«¤Ä¡¢¤½¤ì¤¾¤ì¤Î°Û¤Ê¤ëÊÖ¤êÃÍ¤Î·¿¤È¤¹¤ë¤³¤È¤¬²ÄÇ½¤È¤Ê¤ë¡£
The elements of the `Args` tuple are always non-reference types. Moreover, the element types can have a const or volatile qualifier (jointly referred to as cv-qualifiers), or both. This is since the cv-qualifiers in the arguments can affect the return type. The reason for including the potentially cv-qualified function object type itself into the `Args` tuple, is that the function object class can contain both const and non-const (or volatile, even const volatile) function call operators, and they can each have a different return type.

`sig` ¥Æ¥ó¥×¥ì¡¼¥È¤Ï°ú¿ô¤Î·¿¤Î¥¿¥×¥ë¤«¤é¡¢¤½¤Î·¿¤Î°ú¿ô¤ò¤È¤â¤Ê¤Ã¤Æ¸Æ¤Ð¤ì¤¿¾ì¹ç¤ÎÊÖ¤êÃÍ¤Î·¿¤Ø¤Î¥á¥¿´Ø¿ô¤È¸«¤ë¤³¤È¤¬¤Ç¤¤ë¡£ ¾å¤Ç¼¨¤·¤¿Îã¤Î¤è¤¦¤Ë¡¢¥Æ¥ó¥×¥ì¡¼¥È¤Ï·ë¶É²¿¤«¤ÈÊ£»¨¤Ë¤Ê¤Ã¤Æ¤·¤Þ¤¦¤³¤È¤¬¤¢¤ë¡£ Åµ·¿Åª¤Ê¾ì¹ç¤Ë¼Â¹Ô¤µ¤ì¤ë¤Î¤Ï¡¢const/volatile ½¤¾þ»Ò¤ò¼è¤ê½ü¤¯¤Ê¤É¤·¤Æ¡¢¥¿¥×¥ë¤«¤é´Ø·¸¤Î¤¢¤ë·¿¤ò¼è¤ê½Ð¤¹¤³¤È¤Ç¤¢¤ë¡£ ¤³¤ì¤é¤Î¤³¤È¤Î½õ¤±¤È¤Ê¤ë¼êÃÊ¤Ë¤Ä¤¤¤Æ¤Ï¡¢Boost ¤Î type_traits [type_traits] ¤È Tuple [type_traits] ¤Î¥é¥¤¥Ö¥é¥ê¤ò¸«¤è¡£ `sig` ¥Æ¥ó¥×¥ì¡¼¥È¤ÏºÇ½é¤Ë FC++ ¥é¥¤¥Ö¥é¥ê [fc++] ¤ÇÆ³Æþ¤µ¤ì¤¿Æ±ÍÍ¤Ê»ÅÁÈ¤ò ÀöÎý¤·¤¿¤â¤Î¤Ç¤¢¤ë¡£

The `sig` template can be seen as a meta-function that maps the argument type tuple to the result type of the call made with arguments of the types in the tuple. As the example above demonstrates, the template can end up being somewhat complex. Typical tasks to be performed are the extraction of the relevant types from the tuple, removing cv-qualifiers etc. See the Boost type_traits [type_traits] and Tuple [type_traits] libraries for tools that can aid in these tasks. The `sig` templates are a refined version of a similar mechanism first introduced in the FC++ library [fc++].

½é´ü¤Î¥Ð¡¼¥¸¥ç¥ó¤Î¥é¥¤¥Ö¥é¥ê¤Ç¤Ï¡¢É¸½à¥é¥¤¥Ö¥é¥ê¤Î»ÅÍÍ¤ò¥Ç¥Õ¥©¥ë¥È¤Ç¥µ¥Ý¡¼¥È¤·¤Æ¤ª¤ê¡¢`sig` ¥Æ¥ó¥×¥ì¡¼¥È¤òÇ§¼±¤µ¤»¤ë¤¿¤á¤Ë¤ÏÆÃÊÌ¤ÊÁàºî¤¬É¬Í×¤Ç¤¢¤Ã¤¿¡£ ¸½ºß¤Î BLL ¤Ç¤Ï¡¢¤³¤ì¤¬µÕ¤Ë¤Ê¤Ã¤Æ¤¤¤ë¡£ É¸½à¥é¥¤¥Ö¥é¥ê¤Î»ÅÍÍ¤Ë½¾¤Ã¤¿¥Õ¥¡¥ó¥¯¥¿¤ò bind ¼°¤Ç»ÈÍÑ¤¹¤ëÉ¬Í×¤¬¤¢¤ë¤È¤¤Î¤¿¤á¤Ë¡¢ `std_functor` ¥é¥Ã¥Ñ ¤¬Äó¶¡¤µ¤ì¤Æ¤¤¤ë¡£ ¤³¤Î¥é¥Ã¥Ñ¤Ë¤è¤ê¡¢`result_type` typedef ¤Ë´ð¤Å¤¤¤Æ¡¢`sig` ¥Æ¥ó¥×¥ì¡¼¥È¤¬ÍÑ°Õ¤µ¤ì¤ë¡£ Îã¤¨¤Ð¡¢¼¡¤Î¤è¤¦¤Ë¤Ê¤ë¡£

    int i = 1;
    bind(plus<int>(), _1, 1)(i);              // error, no sig template
    bind(std_functor(plus<int>()), _1, 1)(i); // ok

Earlier versions of the library supported the Standard Library convention as the default, and required special actions to make the library recognize the `sig` template. Now the BLL has that reversed. If one needs to use a functor that adheres to the Standard Library convention in a bind expression, we provide the `std_functor` wrapper, that gives the function object a `sig` template based on the `result_type` typedef. For example:

    int i = 1;
    bind(plus<int>(), _1, 1)(i);              // error, no sig template
    bind(std_functor(plus<int>()), _1, 1)(i); // ok

### 5.4. Overriding the deduced return type

ÊÖ¤êÃÍ¤Î·¿¿äÏÀ¥·¥¹¥Æ¥à¤Ï°ìÉô¤Î¥æ¡¼¥¶ÄêµÁ¤Î±é»»»Ò¤È¥¯¥é¥¹¥ª¥Ö¥¸¥§¥¯¥È¤ò´Þ¤ó¤À bind ¼°¤ÎÊÖ¤êÃÍ¤Î·¿¤ò²ò·è¤Ç¤¤Ê¤¤¤«¤â¤·¤ì¤Ê¤¤¡£ ÊÖ¤êÃÍ¤Î·¿¤òÌÀ¼¨¤·¡¢·¿¿äÏÀ¥·¥¹¥Æ¥à¤ò¥ª¡¼¥Ð¡¼¥é¥¤¥É¤¹¤ë¤¿¤á¤Ë¡¢ÆÃÊÌ¤Ê¦Ë¼°¤Î·¿¤¬Äó¶¡¤µ¤ì¤Æ¤¤¤ë¡£ ¦Ë¼° `e` ¤ÇÄêµÁ¤µ¤ì¤ë¦Ë¥Õ¥¡¥ó¥¯¥¿¤ÎÊÖ¤êÃÍ¤¬ `T` ¤Ç¤¢¤ë¤È¼¨¤¹¤Ë¤Ï¡¢¼¡¤Î¤è¤¦¤Ëµ½Ò¤¹¤ì¤Ð¤è¤¤¡£

    ret<T>(e);

The return type deduction system may not be able to deduce the return types of some user defined operators or bind expressions with class objects. A special lambda expression type is provided for stating the return type explicitly and overriding the deduction system. To state that the return type of the lambda functor defined by the lambda expression `e` is `T`, you can write:

    ret<T>(e);

¤³¤ì¤Ë¤è¤ê¡¢¦Ë¼° `e` ¤Ë¤ÏÊÖ¤êÃÍ¤Î·¿¿äÏÀ¤¬°ìÀÚ¹Ô¤ï¤ì¤Ê¤¤¡£¤½¤Î¤«¤ï¤ê¤Ë¡¢`T` ¤¬ÊÖ¤êÃÍ¤È¤·¤Æ»ÈÍÑ¤µ¤ì¤ë¡£ ÌÀ¤«¤Ë¡¢`T` ¤Ë¤ÏÇ¤°Õ¤Î·¿¤¬»ØÄê¤Ç¤¤ë¤ï¤±¤Ç¤Ï¤Ê¤¤¡£ ¿¿¤Î¦Ë¥Õ¥¡¥ó¥¯¥¿¤Î·¿¤Ï°ÅÌÛÅª¤Ë `T` ¤ËÊÑ´¹²ÄÇ½¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£ Îã¤¨¤Ð¡¢¼¡¤Î¤è¤¦¤Ê¤³¤È¤Ç¤¢¤ë¡£

    A a; B b;
    C operator+(A, B);
    int operator*(A, B);
      ...
    ret<D>(_1 + _2)(a, b);     // error (C cannot be converted to D)
    ret<C>(_1 + _2)(a, b);     // ok
    ret<float>(_1 * _2)(a, b); // ok (int can be converted to float)
      ...
    struct X {
      Y operator(int)();
    };
      ...
    X x; int i;
    bind(x, _1)(i);            // error, return type cannot be deduced
    ret<Y>(bind(x, _1))(i);    // ok

The effect is that the return type deduction is not performed for the lambda expression `e` at all, but instead, `T` is used as the return type. Obviously `T` cannot be an arbitrary type, the true result of the lambda functor must be implicitly convertible to `T`. For example:

    A a; B b;
    C operator+(A, B);
    int operator*(A, B);
      ...
    ret<D>(_1 + _2)(a, b);     // error (C cannot be converted to D)
    ret<C>(_1 + _2)(a, b);     // ok
    ret<float>(_1 * _2)(a, b); // ok (int can be converted to float)
      ...
    struct X {
      Y operator(int)();
    };
      ...
    X x; int i;
    bind(x, _1)(i);            // error, return type cannot be deduced
    ret<Y>(bind(x, _1))(i);    // ok

bind ¼°¤Ë´Ø¤·¤Æ¤Ï¡¢`ret` ¤ÎÂå¤ï¤ê¤Ë»ÈÍÑ¤Ç¤¤ë´ÊÊØ¤ÊÉ½µ¤¬¤¢¤ë¡£ Àè¤ÎÎã¤ÎºÇ¸å¤Î¹Ô¤Ï¡¢¼¡¤Î¤è¤¦¤Ëµ½Ò¤¹¤ë¤³¤È¤¬²ÄÇ½¤Ç¤¢¤ë¡£

    bind<Z>(x, _1)(i);

¤³¤Îµ¡Ç½¤Ï Boost Bind library [bind] ¤ËÊï¤Ã¤¿¤â¤Î¤Ç¤¢¤ë¡£
For bind expressions, there is a short-hand notation that can be used instead of `ret`. The last line could alternatively be written as:

    bind<Z>(x, _1)(i);

This feature is modeled after the Boost Bind library [bind].

¥Í¥¹¥È¤·¤¿¦Ë¼°Ãæ¤Ë¤ª¤¤¤Æ¤Ï¡¢ `ret` ¤Ï¤½¤ì¤Ê¤·¤Ç¤ÏÊÖ¤êÃÍ¤Î·¿¿äÏÀ¤Ë¼ºÇÔ¤¹¤ë¤½¤ì¤¾¤ì¤ÎÉôÊ¬¼°¤Ë¤Ä¤¤¤Æ»ÈÍÑ¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£ Îã¤¨¤Ð¼¡¤Î¤è¤¦¤Ê¤³¤È¤Ç¤¢¤ë¡£

    A a; B b;
    C operator+(A, B); D operator-(C);
      ...
    ret<D>( - (_1 + _2))(a, b); // error
    ret<D>( - ret<C>(_1 + _2))(a, b); // ok

Note that within nested lambda expressions, the `ret` must be used at each subexpression where the deduction would otherwise fail. For example:

    A a; B b;
    C operator+(A, B); D operator-(C);
      ...
    ret<D>( - (_1 + _2))(a, b); // error
    ret<D>( - ret<C>(_1 + _2))(a, b); // ok

¤â¤·¡¢`ret` ¤òÆ±°ì¤Î·¿¤Ë²¿ÅÙ¤â»ÈÍÑ¤·¤Æ¤¤¤ë¤Î¤Ç¤¢¤ì¤Ð¡¢ ÊÖ¤êÃÍ¤Î·¿¿äÏÀ¤ò³ÈÄ¥¤¹¤ë²ÁÃÍ¤¬¤¢¤ë¡£ (¾Ü¤·¤¯¤Ï Section 6 ¤ò¸«¤è)

If you find yourself using `ret` repeatedly with the same types, it is worth while extending the return type deduction (see Section 6).

#### 5.4.1. Nullary lambda functors and ret

¤³¤³¤Þ¤Ç¤Ç¼¨¤·¤¿¤è¤¦¤Ë¡¢`ret` ¤Ë¤è¤êÊÖ¤êÃÍ¤Î·¿¿äÏÀ¤ò¼Â¹Ô¤¹¤ë¤³¤È¤òËÉ¤°¤³¤È¤¬¤Ç¤¤ë¡£ ¤·¤«¤·¡¢Îã³°¤â¤¢¤ë¡£ C++ ¤Î¥Æ¥ó¥×¥ì¡¼¥È¤Î¥¤¥ó¥¹¥¿¥ó¥¹²½¤ÎÆ¯¤¤Î¤¿¤á¤Ë¡¢¥³¥ó¥Ñ¥¤¥é¤Ï 0 °ú¿ô¤Î¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ë´Ø¤·¤Æ¤Ï¡¢¾ï¤ËÊÖ¤êÃÍ¤Î·¿¤¬²ò·è¤µ¤ì¤¿¥Æ¥ó¥×¥ì¡¼¥È¤Î¥¤¥ó¥¹¥¿¥ó¥¹²½¤ò¶¯À©¤¹¤ë¡£ ¤³¤ì¤Ë¤è¤ê¡¢`ret` ¤Ë¤Á¤ç¤Ã¤È¤·¤¿ÌäÂê¤¬È¯À¸¤¹¤ë¡£ ¼¡¤ÎÎã¤¬¤½¤ì¤ò¤è¤¯É½¤ï¤·¤Æ¤¤¤ë¡£

    struct F { int operator()(int i) const; };
    F f;
      ...
    bind(f, _1);           // fails, cannot deduce the return type
    ret<int>(bind(f, _1)); // ok
      ...
    bind(f, 1);            // fails, cannot deduce the return type
    ret<int>(bind(f, 1));  // fails as well!

As stated above, the effect of `ret` is to prevent the return type deduction to be performed. However, there is an exception. Due to the way the C++ template instantiation works, the compiler is always forced to instantiate the return type deduction templates for zero-argument lambda functors. This introduces a slight problem with `ret`, best described with an example:

    struct F { int operator()(int i) const; };
    F f;
      ...
    bind(f, _1);           // fails, cannot deduce the return type
    ret<int>(bind(f, _1)); // ok
      ...
    bind(f, 1);            // fails, cannot deduce the return type
    ret<int>(bind(f, 1));  // fails as well!

BLL ¤Ï `F` ¤Ï `result_type` typedef ¤òÄêµÁ¤·¤Æ¤¤¤Ê¤¤¤¿¤á¡¢ ¾å¤Î¤è¤¦¤Ê bind ¤Î¸Æ½Ð¤·¤ÎÊÖ¤êÃÍ¤Î·¿¤ò¿äÏÀ¤¹¤ë¤³¤È¤Ï¤Ç¤¤Ê¤¤¡£ `ret` ¤¬¤³¤ì¤ò²ò·è¤·¤Æ¤¯¤ì¤ë¤À¤í¤¦¤È´üÂÔ¤¹¤ë¤«¤â¤·¤ì¤Ê¤¤¤¬¡¢ (¾å¤ÎÎã¤ÎºÇ¸å¤Î¹Ô¤Î¤è¤¦¤Ê) bind ¼°¤Î·ë²Ì¤Ç¤¢¤ë°ú¿ô¤Ê¤·¤Î¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ë´Ø¤·¤Æ¤Ï¡¢¤¦¤Þ¤¯µ¡Ç½¤·¤Ê¤¤¤Î¤Ç¤¢¤ë¡£ ¤¿¤È¤¨ÉÔÉ¬Í×¤Ç¤¢¤Ã¤¿¤È¤·¤Æ¤â¡¢ÊÖ¤êÃÍ¤Î·¿¤¬²ò·è¤µ¤ì¤¿¥Æ¥ó¥×¥ì¡¼¥È¤¬¥¤¥ó¥¹¥¿¥ó¥¹²½¤µ¤ì¤ë¡£¤½¤Î·ë²Ì¤È¤·¤Æ¥³¥ó¥Ñ¥¤¥ë»þ¥¨¥é¡¼¤È¤Ê¤ë¡£
The BLL cannot deduce the return types of the above bind calls, as `F` does not define the typedef `result_type`. One would expect `ret` to fix this, but for the nullary lambda functor that results from a bind expression (last line above) this does not work. The return type deduction templates are instantiated, even though it would not be necessary and the result is a compilation error.

¤³¤ÎÌäÂê¤Î²ò·èË¡¤Ï´Ø¿ô `ret` ¤ò»ÈÍÑ¤¹¤ë¤³¤È¤Ç¤Ï¤Ê¤¯¡¢¼¡¤Î¤è¤¦¤Ë `bind` ¤Î¸Æ½Ð¤·¤Ë¤ª¤¤¤Æ¡¢¥Æ¥ó¥×¥ì¡¼¥È°ú¿ô¤ÇÌÀ¼¨Åª¤ËÊÖ¤êÃÍ¤Î·¿¤ò»ØÄê¤¹¤ë¤³¤È¤Ç¤¢¤ë¡£

    bind<int>(f, 1);       // ok

`ret<`T`>(bind(`arg-list`))` ¤«¤éÀ¸À®¤µ¤ì¤¿¦Ë¥Õ¥¡¥ó¥¯¥¿¤È¡¢`bind<`T`>(`arg-list`)` ¤«¤éÀ¸À®¤µ¤ì¤¿¦Ë¥Õ¥¡¥ó¥¯¥¿¤ÏÁ´¤¯Æ±°ì¤Îµ¡Ç½¤ò»ý¤Ä¡£ — °Û¤Ê¤ë¤Î¤ÏÁ°¼Ô¤Ï°ìÉô¤ÎÌµ°ú¿ô¤Î¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ë¤ª¤¤¤Æ¤¦¤Þ¤¯Æ°ºî¤·¤Ê¤¤¤Î¤ËÂÐ¤·¡¢¸å¼Ô¤Ï¤½¤Î¾ì¹ç¤Ë¤âÆ°ºî¤¹¤ë¤È¤¤¤¦¤³¤È¤Ç¤¢¤ë¡£

The solution to this is not to use the `ret` function, but rather define the return type as an explicitly specified template parameter in the `bind` call:

    bind<int>(f, 1);       // ok

The lambda functors created with `ret<`T`>(bind(`arg-list`))` and `bind<`T`>(`arg-list`)` have the exact same functionality — apart from the fact that for some nullary lambda functors the former does not work while the latter does.

### 5.5. Delaying constants and variables

Ã±¹à´Ø¿ô `constant` ¤È `constant_ref` ¤È `var` ¤Ï°ú¿ô¤òÆ±°ì¤Î¥Þ¥Ã¥Ô¥ó¥°¤ò¼ÂÁõ¤¹¤ë ¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ø¤ÈÊÑ²½¤µ¤»¤ë¡£ Á°¤ÎÆó¤Ä¤ÏÄê¿ô¤ËÍÑ¤¤¡¢ºÇ¸å¤Î¤â¤Î¤ÏÊÑ¿ô¤ËÍÑ¤¤¤ë¡£ ¤³¤ì¤é¤ÎÉ¾²Á¤òÃÙ±ä¤µ¤»¤¿Äê¿ô¤äÊÑ¿ô¤Ï¡¢ÌÀ¼¨Åª¤Ê¦Ë¼°¤ÎÊ¸Ë¡¤¬¤Ê¤¤¤¿¤á¤ËÉ¬Í×¤È¤Ê¤ë¡£ Îã¤òµó¤²¤ë¤È¡¢

    for_each(a.begin(), a.end(), cout << _1 << ' ');
    for_each(a.begin(), a.end(), cout << ' ' << _1);

The unary functions `constant`, `constant_ref` and `var` turn their argument into a lambda functor, that implements an identity mapping. The former two are for constants, the latter for variables. The use of these delayed constants and variables is sometimes necessary due to the lack of explicit syntax for lambda expressions. For example:

    for_each(a.begin(), a.end(), cout << _1 << ' ');
    for_each(a.begin(), a.end(), cout << ' ' << _1);

ºÇ½é¤Î¹Ô¤Ç¤Ï¡¢`a`¤ÎÍ×ÁÇ¤ò¶õÇò¤Ç¶èÀÚ¤Ã¤Æ½ÐÎÏ¤¹¤ë¤¬¡¢ Æó¹ÔÌÜ¤Ç¤Ï¡¢°ì¤Ä¶õÇò¤ò½ÐÎÏ¤·¤½¤Î¤¢¤È¤Ë¶èÀÚ¤Ê¤·¤Ç¡¢`a`¤ÎÍ×ÁÇ¤ò½ÐÎÏ¤¹¤ë¡£ ¤³¤ì¤Ï¡¢`cout << ' '` ¤Î¥ª¥Ú¥é¥ó¥É¤Î¤¤¤º¤ì¤â¦Ë¼°¤Ç¤Ê¤¤¤¿¤á¤Ç¤¢¤ë¡£ ¤è¤Ã¤Æ¡¢`cout << ' '` ¤¬Â¨ºÂ¤ËÉ¾²Á¤µ¤ì¤Æ¤·¤Þ¤¦¤Î¤Ç¤¢¤ë¡£ `cout << ' '` ¤ÎÉ¾²Á¤òÃÙ±ä¤µ¤»¤ë¤¿¤á¤Ë¤Ï¡¢¥ª¥Ú¥é¥ó¥É¤Î°ì¤Ä¤ò¦Ë¼°¤Ç¤¢¤ë¤ÈÌÀ¼¨Åª¤Ë¼¨¤µ¤Ê¤¯¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£ ¤³¤ì¤Ï¡¢´Ø¿ô `constant` ¤Ë¤è¤Ã¤Æ¹Ô¤Ê¤¨¤ë¡£

    for_each(a.begin(), a.end(), cout << constant(' ') << _1);

The first line outputs the elements of `a` separated by spaces, while the second line outputs a space followed by the elements of `a` without any separators. The reason for this is that neither of the operands of `cout << ' '` is a lambda expression, hence `cout << ' '` is evaluated immediately. To delay the evaluation of `cout << ' '`, one of the operands must be explicitly marked as a lambda expression. This is accomplished with the `constant` function:

    for_each(a.begin(), a.end(), cout << constant(' ') << _1);

`constant(' ')`¤Î¸Æ½Ð¤·¤Ë¤è¤ê¡¢Ìµ°ú¿ô¤Î¦Ë¥Õ¥¡¥ó¥¯¥¿¤¬À¸À®¤µ¤ì¤ë¡£ ¤³¤Î¥Õ¥¡¥ó¥¯¥¿¤Ï¡¢Äê¿ôÊ¸»ú`' '`¤ò³ÊÇ¼¤·¡¢¸Æ½Ð¤µ¤ì¤¿¤È¤¤Ë¤³¤ÎÊ¸»ú¤Ø¤Î»²¾È¤òÊÖ¤¹¡£ ´Ø¿ô `constant_ref` ¤â¡¢°ú¿ô¤ÎÄê¿ô¤Ø¤Î»²¾È¤ò³ÊÇ¼¤¹¤ë¤È¤¤¤¦ÅÀ¤ò½ü¤¤¤Æ¡¢Æ±ÍÍ¤Ç¤¢¤ë¡£ `constant` ¤È `constant_ref` ¤Ï¾åµ¤ÎÎã¤Î¤è¤¦¤Ë¡¢±é»»»Ò¤Î¸Æ½Ð¤·¤ËÉûºîÍÑ¤¬¤¢¤ë¤È¤¤Ë¤Î¤ßÉ¬Í×¤È¤Ê¤ë¡£
The call `constant(' ')` creates a nullary lambda functor which stores the character constant `' '` and returns a reference to it when invoked. The function `constant_ref` is similar, except that it stores a constant reference to its argument. The `constant` and `consant_ref` are only needed when the operator call has side effects, like in the above example.

»þ¡¹ÊÑ¿ô¤ÎÉ¾²Á¤òÃÙ±ä¤µ¤»¤ëÉ¬Í×¤¬¤¢¤ë¡£ ¥³¥ó¥Æ¥ó¥Ê¤ÎÍ×ÁÇ¤òÈÖ¹æÉÕ¤±¤·¤¿¥ê¥¹¥È¤È¤·¤Æ½ÐÎÏ¤·¤¿¤¤¤È¤¹¤ë¡£

    int index = 0;
    for_each(a.begin(), a.end(), cout << ++index << ':' << _1 << '\n');
    for_each(a.begin(), a.end(), cout << ++var(index) << ':' << _1 << '\n');

ºÇ½é¤Î¹Ô¤Î `for_each` ¤Î¸Æ½Ð¤·¤Ï´üÂÔÄÌ¤ê¤Ë¤ÏÆ°ºî¤·¤Ê¤¤¡£ `index` ¤Ï°ìÅÙ¤À¤±¥¤¥ó¥¯¥ê¥á¥ó¥È¤µ¤ì¡¢¤½¤ÎÃÍ¤¬°ìÅÙ¤À¤±½ÐÎÏ¥¹¥È¥ê¡¼¥à¤Ë½ñ¤½Ð¤µ¤ì¤ë¡£ `var` ¤ò»ÈÍÑ¤·¤Æ¡¢`index` ¤ò¦Ë¼°¤È¤¹¤ë¤³¤È¤Ë¤è¤ê¡¢Ë¾¤à¸ú²Ì¤¬ÆÀ¤é¤ì¤ë¡£

Sometimes we need to delay the evaluation of a variable. Suppose we wanted to output the elements of a container in a numbered list:

    int index = 0;
    for_each(a.begin(), a.end(), cout << ++index << ':' << _1 << '\n');
    for_each(a.begin(), a.end(), cout << ++var(index) << ':' << _1 << '\n');

The first `for_each` invocation does not do what we want; `index` is incremented only once, and its value is written into the output stream only once. By using `var` to make `index` a lambda expression, we get the desired effect.

¤Þ¤È¤á¤ë¤È¡¢`var(x)` ¤ÏÊÑ¿ô `x`¤Ø¤Î»²¾È¤ò³ÊÇ¼¤·¤¿Ìµ°ú¿ô¤Î¦Ë¥Õ¥¡¥ó¥¯¥¿¤òÀ¸À®¤¹¤ë¡£ ¤³¤Î¦Ë¥Õ¥¡¥ó¥¯¥¿¤¬¸Æ½Ð¤µ¤ì¤ë¤È¡¢`x` ¤Ø¤Î»²¾È¤¬ÊÖ¤µ¤ì¤ë¡£

In sum, `var(x)` creates a nullary lambda functor, which stores a reference to the variable `x`. When the lambda functor is invoked, a reference to `x` is returned.

#### Naming delayed constants and variables

¦Ë¼°¤Î³°Â¦¤ÇÃÙ±äÉ¾²Á¤¹¤ëÊÑ¿ô¤äÄê¿ô¤òÁ°¤â¤Ã¤ÆÄêµÁ¤·¡¢Ì¾Á°ÉÕ¤±¤¹¤ë¤³¤È¤â²ÄÇ½¤Ç¤¢¤ë¡£ ¥Æ¥ó¥×¥ì¡¼¥È `var_type` ¤ä `constant_type` ¤ä `constant_ref_type` ¤Ï¤³¤ÎÌÜÅª¤Î¤¿¤á¤ËÄó¶¡¤µ¤ì¤Æ¤¤¤ë¡£ ¤³¤ì¤é¤Ï¼¡¤Î¤è¤¦¤Ë»ÈÍÑ¤¹¤ë¡£

    var_type<T>::type delayed_i(var(i));
    constant_type<T>::type delayed_c(constant(c));

It is possible to predefine and name a delayed variable or constant outside a lambda expression. The templates `var_type`, `constant_type` and `constant_ref_type` serve for this purpose. They are used as:

    var_type<T>::type delayed_i(var(i));
    constant_type<T>::type delayed_c(constant(c));

ºÇ½é¤Î¹Ô¤Ç¤Ï¡¢·¿ `T` ¤ÎÊÑ¿ô `i` ¤ÎÃÙ±äÉ¾²ÁÍÑ¤ÎÊÑ¿ô `delayed_i` ¤òÄêµÁ¤·¤Æ¤¤¤ë¡£ Æ±ÍÍ¤Ë¡¢Æó¹ÔÌÜ¤Ç¤Ï¡¢Äê¿ô `c` ¤ÎÃÙ±äÉ¾²ÁÍÑ¤ÎÄê¿ô¤È¤·¤Æ `delayed_c`¤òÄêµÁ¤·¤Æ¤¤¤ë¡£ Îã¤¨¤Ð¡¢

    int i = 0; int j;
    for_each(a.begin(), a.end(), (var(j) = _1, _1 = var(i), var(i) = var(j)));

¤Ï¼¡¤Î¤â¤Î¤ÈÅù²Á¤Ç¤¢¤ë¡£

    int i = 0; int j;
    var_type<int>::type vi(var(i)), vj(var(j));
    for_each(a.begin(), a.end(), (vj = _1, _1 = vi, vi = vj));

The first line defines the variable `delayed_i` which is a delayed version of the variable `i` of type `T`. Analogously, the second line defines the constant `delayed_c` as a delayed version of the constant `c`. For example:

    int i = 0; int j;
    for_each(a.begin(), a.end(), (var(j) = _1, _1 = var(i), var(i) = var(j)));

is equivalent to:

    int i = 0; int j;
    var_type<int>::type vi(var(i)), vj(var(j));
    for_each(a.begin(), a.end(), (vj = _1, _1 = vi, vi = vj));

ÃÙ±äÉ¾²Á¤¹¤ëÄê¿ô¤ÎÌ¾Á°ÉÕ¤±¤ÎÎã¤òµó¤²¤ë¤È¡¢

    constant_type<char>::type space(constant(' '));
    for_each(a.begin(),a.end(), cout << space << _1);

Here is an example of naming a delayed constant:

    constant_type<char>::type space(constant(' '));
    for_each(a.begin(),a.end(), cout << space << _1);

#### About assignment and subscript operators

Section 5.2.2 ¤Ç¼¨¤·¤¿¤è¤¦¤Ë¡¢ÂåÆþ¤ÈÅº»ú¤Î±é»»»Ò¤Ï¾ï¤Ë¥á¥ó¥Ð´Ø¿ô¤È¤·¤ÆÄêµÁ¤µ¤ì¤ë¡£ ¤³¤Î¤¿¤á¡¢ `x = y` ¤ä `x[y]` ¤Î·Á¤Î¼°¤¬¦Ë¼°¤È²ò¼á¤µ¤ì¤ë¤¿¤á¤Ë¤Ï¡¢º¸Â¦¤Î¥ª¥Ú¥é¥ó¥É¤Ï¦Ë¼°¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£ ·ë²Ì¤È¤·¤Æ¡¢`var`¤ò¤³¤ÎÌÜÅª¤Î¤¿¤á¤Ë»ÈÍÑ¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¾ì¹ç¤¬¤¢¤ë¡£ ¼¡¤Ë¡¢Section 5.2.2¤ÎÎã¤òºÆ¤Ó¼¨¤¹¡£

    int i;
    i = _1;       // error
    var(i) = _1;  // ok

As described in Section 5.2.2, assignment and subscripting operators are always defined as member functions. This means, that for expressions of the form `x = y` or `x[y]` to be interpreted as lambda expressions, the left-hand operand `x` must be a lambda expression. Consequently, it is sometimes necessary to use `var` for this purpose. We repeat the example from Section 5.2.2:

    int i;
    i = _1;       // error
    var(i) = _1;  // ok

`+=` ¤ä `-=` ¤È¤¤¤Ã¤¿Ê£¹çÂåÆþ±é»»»Ò¤ÏÈó¥á¥ó¥Ð´Ø¿ô¤È¤·¤ÆÄêµÁ¤¹¤ë¤³¤È¤¬²ÄÇ½¤Ç¤¢¤ë¡£¤½¤Î¤¿¤á¡¢¤¿¤È¤¨±¦Â¦¤Î¥ª¥Ú¥é¥ó¥É¤Î¤ß¤¬¦Ë¼°¤Ç¤¢¤Ã¤¿¤È¤·¤Æ¤â¡¢¤³¤ì¤é¤Î¼°¤Ï¦Ë¼°¤È¤·¤Æ²ò¼á¤µ¤ì¤ë¡£ ¤·¤«¤·¤Ê¤¬¤é¡¢º¸Â¦¤Î¥ª¥Ú¥é¥ó¥É¤òÌÀ¼¨Åª¤ËÃÙ±äÉ¾²Á¤µ¤»¤Æ¤âÁ´¤¯ÌäÂê¤Ê¤¤¡£ Îã¤¨¤Ð¡¢`i += _1` ¤È `var(i) += _1` ¤ÏÅù²Á¤Ç¤¢¤ë¡£

Note that the compound assignment operators `+=`, `-=` etc. can be defined as non-member functions, and thus they are interpreted as lambda expressions even if only the right-hand operand is a lambda expression. Nevertheless, it is perfectly ok to delay the left operand explicitly. For example, `i += _1` is equivalent to `var(i) += _1`.

### 5.6. Lambda expressions for control structures

BLL ¤ÏÀ©¸æ¹½Â¤¤òÉ½¸½¤¹¤ë¦Ë¥Õ¥¡¥ó¥¯¥¿¤òÀ¸À®¤¹¤ë¤¤¤¯¤Ä¤«¤Î´Ø¿ô¤òÄêµÁ¤·¤Æ¤¤¤ë¡£ ¤½¤ì¤é¤Ï¡¢°ú¿ô¤È¤·¤Æ¦Ë¥Õ¥¡¥ó¥¯¤ò¼è¤ê¡¢ÊÖ¤êÃÍ¤Ï `void` ¤Ç¤¢¤ë¡£ ¤Þ¤º¡¢Îã¤òÄó¼¨¤¹¤ë¡£°Ê²¼¤Î¥³¡¼¥É¤Ï¤¢¤ë¥³¥ó¥Æ¥Ê `a`¤Î¶ö¿ô¤ÎÍ×ÁÇ¤ò¤¹¤Ù¤Æ½ÐÎÏ¤¹¤ë¡£

    for_each(a.begin(), a.end(),
             if_then(_1 % 2 == 0, cout << _1));

BLL defines several functions to create lambda functors that represent control structures. They all take lambda functors as parameters and return `void`. To start with an example, the following code outputs all even elements of some container `a`:

    for_each(a.begin(), a.end(),
             if_then(_1 % 2 == 0, cout << _1));

BLL ¤ÏÀ©¸æ¹½Â¤¤Î¤¿¤á¤Ë°Ê²¼¤Î´Ø¿ô¥Æ¥ó¥×¥ì¡¼¥È¤ò¥µ¥Ý¡¼¥È¤¹¤ë¡£

    if_then(condition, then_part)
    if_then_else(condition, then_part, else_part)
    if_then_else_return(condition, then_part, else_part)
    while_loop(condition, body)
    while_loop(condition) // no body case
    do_while_loop(condition, body)
    do_while_loop(condition) // no body case
    for_loop(init, condition, increment, body)
    for_loop(init, condition, increment) // no body case
    switch_statement(...)

The BLL supports the following function templates for control structures:

    if_then(condition, then_part)
    if_then_else(condition, then_part, else_part)
    if_then_else_return(condition, then_part, else_part)
    while_loop(condition, body)
    while_loop(condition) // no body case
    do_while_loop(condition, body)
    do_while_loop(condition) // no body case
    for_loop(init, condition, increment, body)
    for_loop(init, condition, increment) // no body case
    switch_statement(...)

¤¹¤Ù¤Æ¤ÎÀ©¸æ¹½Â¤¤Î¦Ë¥Õ¥¡¥ó¥¯¥¿¤ÎÊÖ¤êÃÍ¤Ï `void`¤Ç¤¢¤ë¡£ Îã³°¤Ï¡¢¼¡¤Î¾ò·ï±é»»»Ò¤Î¸Æ½Ð¤·¤ò¥é¥Ã¥×¤¹¤ë `if_then_else_return` ¤À¤±¤Ç¤¢¤ë¡£

    condition ? then_part : else_part

The return types of all control construct lambda functor is `void`, except for `if_then_else_return`, which wraps a call to the conditional operator

    condition ? then_part : else_part

¤³¤Î±é»»»Ò¤ÎÊÖ¤êÃÍ¤Î·¿¤Îµ¬Â§¤Ï¾¯¡¹Ê£»¨¤Ç¤¢¤ë¡£ ´ðËÜÅª¤Ë¤Ï¡¢Î¾Êý¤ÎÊ¬´ô¤Î·ë²Ì¤Î·¿¤¬Æ±°ì¤Ç¤¢¤ì¤Ð¡¢¤½¤Î·¿¤¬ÊÖ¤êÃÍ¤Î·¿¤Ç¤¢¤ë¡£ Ê¬´ô¤Î·¿¤Î·ë²Ì¤¬°Û¤Ê¤ë¤Î¤Ç¤¢¤ì¤Ð¡¢ ÊÒÊý¤ÎÊ¬´ô¤Î·ë²Ì¡¢Îã¤¨¤Ð `A` ¤Ï¤â¤¦°ì¤Ä¤ÎÊ¬´ô¤Î·ë²Ì¡¢Îã¤¨¤Ð `B` ¤ËÊÑ´¹²ÄÇ½¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£ ¤³¤Î¤è¤¦¤Ê¾õ¶·¤Ç¤Ï¡¢ÊÖ¤êÃÍ¤Î·¿¤Ï `B`¤Ç¤¢¤ë¡£ ¤µ¤é¤Ë¡¢¶¦ÄÌ¤Î·¿¤¬º¸ÊÕÃÍ¤Ç¤¢¤ì¤Ð¡¢ÊÖ¤êÃÍ¤âº¸ÊÕÃÍ¤Ë¤Ê¤ë¤Ç¤¢¤í¤¦¡£
The return type rules for this operator are somewhat complex. Basically, if the branches have the same type, this type is the return type. If the type of the branches differ, one branch, say of type `A`, must be convertible to the other branch, say of type `B`. In this situation, the result type is `B`. Further, if the common type is an lvalue, the return type will be an lvalue too.

ÃÙ±äÉ¾²Á¤µ¤ì¤ëÊÑ¿ô¤Ï¡¢À©¸æ¹½Â¤¤Î¦Ë¼°¤Ë¤è¤¯¸½¤ì¤ë¡£ Îã¤¨¤Ð¡¢¤³¤³¤Ç¤Ï¡¢`for_loop` ¤Î°ú¿ô¤ò¦Ë¼°¤ËÊÑ´¹¤¹¤ë¤¿¤á¤Ë `var`¤ò»ÈÍÑ¤·¤Æ¤¤¤ë¡£ ¼¡¤Î¥³¡¼¥É¤ÏÆó¼¡¸µÇÛÎó¤Î³ÆÍ×ÁÇ¤Ë 1 ¤ò²Ã¤¨¤ë¤â¤Î¤Ç¤¢¤ë¡£

    int a[5][10]; int i;
    for_each(a, a+5,
      for_loop(var(i)=0, var(i)<10, ++var(i),
               _1[var(i)] += 1));

Delayed variables tend to be commonplace in control structure lambda expressions. For instance, here we use the `var` function to turn the arguments of `for_loop` into lambda expressions. The effect of the code is to add 1 to each element of a two-dimensional array:

    int a[5][10]; int i;
    for_each(a, a+5,
      for_loop(var(i)=0, var(i)<10, ++var(i),
               _1[var(i)] += 1));

BLL ¤Ï Joel de Guzmann ¤¬Äó°Æ¤·¤¿¡¢ÊÌ¤ÊÀ©¸æ¼°¤ÎÊ¸Ë¡¤â¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤ë¡£ `operator[]` ¤ò¥ª¡¼¥Ð¡¼¥í¡¼¥É¤¹¤ë¤³¤È¤Ë¤è¤ê¡¢ ÁÈ¤ß¹þ¤ß¤ÎÀ©¸æ¹½Â¤¤ÈÈó¾ï¤Ë¤è¤¯»÷¤¿¤â¤Î¤ò¼Â¸½¤Ç¤¤ë¡£

    if_(condition)[then_part]
    if_(condition)[then_part].else_[else_part]
    while_(condition)[body]
    do_[body].while_(condition)
    for_(init, condition, increment)[body]

Îã¤¨¤Ð¡¢¤³¤ÎÊ¸Ë¡¤ò»ÈÍÑ¤¹¤ë¤È ¾åµ¤Î `if_then` ¤ÎÎã¤Ï¼¡¤Î¤è¤¦¤Ëµ½Ò¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£

    for_each(a.begin(), a.end(),
             if(_1 % 2 == 0)[ cout << _1 ])

·Ð¸³¤òÀÑ¤à¤È¡¢¤³¤ì¤é¤Î¤¤¤¯¤Ä¤«¤ò·ë¶É¤ÏÈóÆñ¤¹¤ë¤«¤â¤·¤ì¤Ê¤¤¡£

The BLL supports an alternative syntax for control expressions, suggested by Joel de Guzmann. By overloading the `operator[]` we can get a closer resemblance with the built-in control structures:

    if_(condition)[then_part]
    if_(condition)[then_part].else_[else_part]
    while_(condition)[body]
    do_[body].while_(condition)
    for_(init, condition, increment)[body]

For example, using this syntax the `if_then` example above can be written as:

    for_each(a.begin(), a.end(),
             if(_1 % 2 == 0)[ cout << _1 ])

As more experience is gained, we may end up deprecating one or the other of these syntaces.

#### 5.6.1. Switch statement

¿ôÂ¿¤¯¤Î¾ì¹ç¤¬¤¢¤ë¤¿¤á¡¢`switch` Ê¸¤ÎÀ©¸æ¹½Â¤¤Î¦Ë¼°¤Ï¤â¤¦¾¯¤·Ê£»¨¤Ç¤¢¤ë¡£ °ìÈÌÅª¤Ê switch Ê¸¤Î¦Ë¼°¤Î·Á¤Ï¼¡¤Î¤è¤¦¤Ë¤Ê¤ë¡£

    switch_statement(condition,
      case_statement<label>(lambda expression),
      case_statement<label>(lambda expression),
      ...
      default_statement(lambda expression)
    )

``condition`` °ú¿ô¤ÏÁ´ÂÎ¤ÎÊÖ¤êÃÍ¤Î·¿¤ò¤È¤â¤Ê¤Ã¤¿¦Ë¥Õ¥¡¥ó¥¯¥¿¤òÀ¸À®¤¹¤ë¦Ë¼°¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£ ´Ø¿ô `case_statement` ¤Ë¤è¤Ã¤Æ³Æ case Ê¸¤¬À¸À®¤µ¤ì¡¢ ´Ø¿ô `default_statement` ¤Ë¤è¤Ã¤Æ¥ª¥×¥·¥ç¥ó¤Ç¤¢¤ë default Ê¸¤¬À¸À®¤µ¤ì¤ë¡£ case Ê¸¤Î¥é¥Ù¥ë¤Ï´Ø¿ô `case_statement` ¤Ø¤Î¥Æ¥ó¥×¥ì¡¼¥È°ú¿ô¤Ë¤è¤Ã¤ÆÌÀ¼¨Åª¤Ë»ØÄê¤µ¤ì¡¢`break` Ê¸¤Ï°ÅÌÛÅª¤Ë³Æ case Ê¸¤Ë´Þ¤Þ¤ì¤Æ¤¤¤ë¡£ Îã¤¨¤Ð¡¢`a` ¤¬¤¢¤ë¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ç¤¢¤ë¤È¤·¤Æ¡¢`case_statement<1>(a)` ¤Ï¼¡¤Î¤è¤¦¤Ê¥³¡¼¥É¤òÀ¸À®¤¹¤ë¡£

    case 1:
      evaluate lambda functor a;
      break;

´Ø¿ô `switch_statement` ¤Ï 9 ¤Ä¤Þ¤Ç¤Î case Ê¸¤ËÆÃ²½¤µ¤ì¤Æ¤¤¤ë¡£

The lambda expressions for `switch` control structures are more complex since the number of cases may vary. The general form of a switch lambda expression is:

    switch_statement(condition,
      case_statement<label>(lambda expression),
      case_statement<label>(lambda expression),
      ...
      default_statement(lambda expression)
    )

The ``condition`` argument must be a lambda expression that creates a lambda functor with an integral return type. The different cases are created with the `case_statement` functions, and the optional default case with the `default_statement` function. The case labels are given as explicitly specified template arguments to `case_statement` functions and `break` statements are implicitly part of each case. For example, `case_statement<1>(a)`, where `a` is some lambda functor, generates the code:

    case 1:
      evaluate lambda functor a;
      break;

The `switch_statement` function is specialized for up to 9 case statements.

switch Ê¸¤Î´°Á´¤ÊÎã¤ò¼¨¤¹¡£ ¼¡¤Î¥³¡¼¥É¤Ç¤Ï¡¢¤¢¤ë¥³¥ó¥Æ¥Ê `v` ¤Ë´Ø¤·¤ÆÈ¿Éü¤·¤Ê¤¬¤é¡¢0 ¤ÎÍ×ÁÇ¤´¤È¤Ë “zero” ¤ò¡¢ 1 ¤ÎÍ×ÁÇ¤´¤È¤Ë “one” ¤ò¡¢¤½¤ÎÂ¾¤Î¿ô `n` ¤ËÂÐ¤·¤Æ¤Ï¡¢“other: `n`” ¤È¤¤¤¦Ê¸»úÎó¤ò½ÐÎÏ¤¹¤ë¡£ ³ÆÍ×ÁÇ¤Î¸å¤Ë²þ¹Ô¤ò½ÐÎÏ¤¹¤ë¤¿¤á¤Ë¡¢`switch_statement` ¤Î¸å¤ËÊÌ¤Ê¦Ë¼°¤¬Â³¤¤¤Æ¤¤¤ë¡£

    std::for_each(v.begin(), v.end(),
      (
        switch_statement(
          _1,
          case_statement<0>(std::cout << constant("zero")),
          case_statement<1>(std::cout << constant("one")),
          default_statement(cout << constant("other: ") << _1)
        ),
        cout << constant("\n")
      )
    );

As a concrete example, the following code iterates over some container `v` and ouptuts “zero” for each `0`, “one” for each `1`, and “other: `n`” for any other value `n`. Note that another lambda expression is sequenced after the `switch_statement` to output a line break after each element:

    std::for_each(v.begin(), v.end(),
      (
        switch_statement(
          _1,
          case_statement<0>(std::cout << constant("zero")),
          case_statement<1>(std::cout << constant("one")),
          default_statement(cout << constant("other: ") << _1)
        ),
        cout << constant("\n")
      )
    );

### 5.7. Exceptions

BLL ¤Ï Îã³°¤òÁ÷½Ð¤·¤¿¤ê¡¢ÊäÂ¤¹¤ë¦Ë¥Õ¥¡¥ó¥¯¥¿¤òÄó¶¡¤·¤Æ¤¤¤ë¡£ Îã³°¤òÁ÷½Ð¤¹¤ë¦Ë¥Õ¥¡¥ó¥¯¥¿¤ÏÃ±¹à´Ø¿ô `throw_exception` ¤Ë¤è¤Ã¤ÆÀ¸À®¤µ¤ì¤ë¡£ ¤³¤Î´Ø¿ô¤Î°ú¿ô¤ÏÁ÷½Ð¤¹¤ëÎã³°¤«¡¢Á÷½Ð¤¹¤ëÎã³°¤òÀ¸À®¤¹¤ë¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ç¤¢¤ë¡£ Îã³°¤òºÆÁ÷½Ð¤¹¤ë¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ï¡¢Ìµ¹à´Ø¿ô `rethrow` ¤Ë¤è¤Ã¤ÆÀ¸À®¤µ¤ì¤ë¡£

The BLL provides lambda functors that throw and catch exceptions. Lambda functors for throwing exceptions are created with the unary function `throw_exception`. The argument to this function is the exception to be thrown, or a lambda functor which creates the exception to be thrown. A lambda functor for rethrowing exceptions is created with the nullary `rethrow` function.

Îã³°¤ò°·¤¦¦Ë¼°¤Ï¤â¤¦¾¯¤·Ê£»¨¤Ç¤¢¤ë¡£ try-catch ¥Ö¥í¥Ã¥¯¤Î°ìÈÌÅª¤Ê¦Ë¼°¤Î·Á¤Ï¼¡¤Î¤è¤¦¤Ë¤Ê¤ë¡£

    try_catch(
      lambda expression,
      catch_exception<type>(lambda expression),
      catch_exception<type>(lambda expression),
      ...
      catch_all(lambda expression)
    )

Lambda expressions for handling exceptions are somewhat more complex. The general form of a lambda expression for try catch blocks is as follows:

    try_catch(
      lambda expression,
      catch_exception<type>(lambda expression),
      catch_exception<type>(lambda expression),
      ...
      catch_all(lambda expression)
    )

ºÇ½é¤Î¦Ë¼°¤¬ try ¥Ö¥í¥Ã¥¯¤Ç¤¢¤ë¡£ ³Æ `catch_exception` ¤ÏÊäÂ¤¹¤ëÎã³°¤Î·¿¤ò¥Æ¥ó¥×¥ì¡¼¥È°ú¿ô¤È¤·¤ÆÌÀ¼¨Åª¤Ë»ØÄê¤·¤Æ catch ¥Ö¥í¥Ã¥¯¤òÄêµÁ¤¹¤ë¡£ `catch_exception` Æâ¤Î¦Ë¼°¤Ç¤Ï¡¢Îã³°¤òÊäÂ¤·¤¿ºÝ¤ÎÆ°ºî¤òÄêµÁ¤¹¤ë¡£ »²¾È¤È¤·¤ÆÊäÂ¤¹¤ëÎã³°¥Ï¥ó¥É¥é¡¢¤¹¤Ê¤ï¤Á¡¢`catch_exception<T>(...)` ¤È¤¤¤¦¦Ë¼°¤Ï¼¡¤Î¤è¤¦¤Ê catch ¥Ö¥í¥Ã¥¯¤È¤Ê¤ë¡£

    catch(T& e) { ... }

The first lambda expression is the try block. Each `catch_exception` defines a catch block where the explicitly specified template argument defines the type of the exception to catch. The lambda expression within the `catch_exception` defines the actions to take if the exception is caught. Note that the resulting exception handlers catch the exceptions as references, i.e., `catch_exception<T>(...)` results in the catch block:

    catch(T& e) { ... }

ºÇ¸å¤Î catch ¥Ö¥í¥Ã¥¯¤Ï¡¢`catch_exception<`type`>` ¤Þ¤¿¤Ï¡¢`catch(...)` ¤ÈÅù²Á¤Î¦Ë¼° `catch_all` ¤Î¸Æ½Ð¤·¤È¤Ê¤ë¡£
The last catch block can alternatively be a call to `catch_exception<`type`>` or to `catch_all`, which is the lambda expression equivalent to `catch(...)`.

Example 1 ¤Ç¤Ï BLL ¤ÎÎã³°Áàºî¤Îµ¡Ç½¤Î»È¤¤Êý¤Î¼ÂÎã¤ò¼¨¤·¤Æ¤¤¤ë¡£ ºÇ½é¤ÎÎã³°¥Ï¥ó¥É¥é¤Ï `foo_exception` ·¿¤ÎÎã³°¤òÊäÂ¤¹¤ë¡£ ¥×¥ì¡¼¥¹¥Û¥ë¥À `_1` ¤ò¥Ï¥ó¥É¥é¤ÎÆâÉô¤Ç»ÈÍÑ¤·¤Æ¤¤¤ë¡£

The Example 1 demonstrates the use of the BLL exception handling tools. The first handler catches exceptions of type `foo_exception`. Note the use of `_1` placeholder in the body of the handler.

Æó¤ÄÌÜ¤ÎÎã³°¥Ï¥ó¥É¥é¤Ç¤Ï¡¢Îã³°¤ÎÁ÷½Ð¤ÎÊýË¡¤È¡¢ Îã³°¥×¥ì¡¼¥¹¥Û¥ë¥À `_e` ¤Î¼ÂÎã¤ò¼¨¤·¤Æ¤¤¤ë¡£ ¤³¤ÎÆÃÊÌ¤Ê¥×¥ì¡¼¥¹¥Û¥ë¥À¤Ï¡¢ÊäÂ¤·¤¿Îã³°¥ª¥Ö¥¸¥§¥¯¥È¤ò¥Ï¥ó¥É¥é¤ÎÆâÉô¤«¤é»²¾È¤·¤Æ¤¤¤ë¡£ ¤³¤³¤Ç¤Ï¡¢Îã³°¤Î¸¶°ø¤òÀâÌÀ¤¹¤ëÊ¸»úÎó¤¬ÉÕ¿ï¤·¤Æ¤¤¤ë `std::exception` ·¿¤ÎÎã³°¤ò°·¤Ã¤Æ¤¤¤ë¡£ ¤³¤ÎÀâÌÀ¤ÏÌµ°ú¿ô¤Î¥á¥ó¥Ð´Ø¿ô `what` ¤Ë¤è¤ê¼è¤ê½Ð¤¹¤³¤È¤¬¤Ç¤¤ë¡£ `bind(&std::exception::what, _e)` ¤È¤¤¤¦¼°¤Ï¤³¤Î´Ø¿ô¤ò¸Æ½Ð¤¹¦Ë¥Õ¥¡¥ó¥¯¥¿¤òÀ¸À®¤¹¤ë¡£ `_e` ¤ÏÎã³°¥Ï¥ó¥É¥é¤ò¼¨¤¹¦Ë¼°¤Î³°Â¦¤Ç¤Ï»ÈÍÑ¤Ç¤¤Ê¤¤¡£ ÆóÈÖÌÜ¤ÎÎã³°¥Ï¥ó¥É¥é¤ÎºÇ¸å¤Î¹Ô¤Ç¤Ï¡¢¿·¤¿¤ÊÎã³°¥ª¥Ö¥¸¥§¥¯¥È¤ò¹½ÃÛ¤·¡¢ ´Ø¿ô `throw_exception` ¤ò»ÈÍÑ¤·¤Æ¡¢¤½¤ÎÎã³°¥ª¥Ö¥¸¥§¥¯¥È¤òÁ÷½Ð¤¹¤ë¡£ ¦Ë¼°Ãæ¤Ë¤ª¤±¤ë¡¢¥ª¥Ö¥¸¥§¥¯¥È¤Î¹½ÃÛ¤ÈÇË´þ¤Ë´Ø¤·¤Æ¤Ï¡¢ Section 5.8 ¤Ë¤ª¤¤¤ÆÀâÌÀ¤¹¤ë¡£

The second handler shows how to throw exceptions, and demonstrates the use of the exception placeholder `_e`. It is a special placeholder, which refers to the caught exception object within the handler body. Here we are handling an exception of type `std::exception`, which carries a string explaining the cause of the exception. This explanation can be queried with the zero-argument member function `what`. The expression `bind(&std::exception::what, _e)` creates the lambda function for making that call. Note that `_e` cannot be used outside of an exception handler lambda expression. The last line of the second handler constructs a new exception object and throws that with `throw exception`. Constructing and destructing objects within lambda expressions is explained in Section 5.8

¤½¤·¤Æ¡¢»°ÈÖÌÜ¤ÎÎã³°¥Ï¥ó¥É¥é(`catch_all`)¤Ç¤Ï¡¢Îã³°¤ÎºÆÁ÷½Ð¤Î¼ÂÎã¤ò¼¨¤¹¡£

Finally, the third handler (`catch_all`) demonstrates rethrowing exceptions.

Example 1. Throwing and handling exceptions in lambda expressions.

    for_each(
      a.begin(), a.end(),
      try_catch(
        bind(foo, _1),                 // foo may throw
        catch_exception<foo_exception>(
          cout << constant("Caught foo_exception: ")
               << "foo was called with argument = " << _1
        ),
        catch_exception<std::exception>(
          cout << constant("Caught std::exception: ")
               << bind(&std::exception::what, _e),
          throw_exception(bind(constructor<bar_exception>(), _1)))
        ),
        catch_all(
          (cout << constant("Unknown"), rethrow())
        )
      )
    );

### 5.8. Construction and destruction

±é»»»Ò `new` ¤È `delete` ¤Ï¥ª¡¼¥Ð¡¼¥í¡¼¥É²ÄÇ½¤Ç¤¢¤ë¤¬¡¢ ¤½¤ì¤é¤ÎÊÖ¤êÃÍ¤Ï¶¦ÄÌ¤Ç¤¢¤ë¡£ ÆÃ¤Ë¡¢ÊÖ¤êÃÍ¤Ï¦Ë¥Õ¥¡¥ó¥¯¥¿¤È¤Ê¤ë¤³¤È¤Ï¤Ê¤¤¡£ ¤½¤Î¤¿¤á¡¢¦Ë¼°¤Î¤¿¤á¤Ë¥ª¡¼¥Ð¡¼¥í¡¼¥É¤µ¤ì¤ë¤³¤È¤¬ËÉ¤²¤ë¡£ ¥³¥ó¥¹¥È¥é¥¯¥¿¤Î¥¢¥É¥ì¥¹¤ò¼èÆÀ¤¹¤ë¤³¤È¤Ï¤Ç¤¤Ê¤¤¡£ ¤è¤Ã¤Æ¡¢¥³¥ó¥¹¥È¥é¥¯¥¿¤Ï bind ¼°¤ÎÂÐ¾Ý¤È¤·¤Æ»ÈÍÑ¤¹¤ë¤³¤È¤Ï¤Ç¤¤Ê¤¤¡£ ¤³¤Î¤³¤È¤Ï¡¢¥Ç¥¹¥È¥é¥¯¥¿¤Ë´Ø¤·¤Æ¤âÆ±¤¸¤Ç¤¢¤ë¡£ ¤³¤Î¤è¤¦¤ÊÀ©¸Â¤ò²óÈò¤¹¤ë¤¿¤á¤Ë¡¢BLL ¤Ï ¥³¥ó¥¹¥È¥é¥¯¥¿¤ä¥Ç¥¹¥È¥é¥¯¥¿¤ÈÆ±ÍÍ¤Ë `new` ¤È `delete` ¤Î¸Æ½Ð¤·¤ò¹Ô¤Ê¤¦¤¿¤á¤Î¥é¥Ã¥Ñ¤Î¥¯¥é¥¹¤ò ÄêµÁ¤·¤Æ¤¤¤ë¡£ ¤³¤ì¤é¤Î¥¯¥é¥¹¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤Ï´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤Ç¤¢¤ê¡¢ bind ¼°¤ÎÂÐ¾Ý¤È¤·¤Æ»ÈÍÑ¤¹¤ë¤³¤È¤¬²ÄÇ½¤Ç¤¢¤ë¡£ ¼¡¤ËÎã¤òµó¤²¤ë¡£

    int* a[10];
    for_each(a, a+10, _1 = bind(new_ptr<int>()));
    for_each(a, a+10, bind(delete_ptr(), _1));

Operators `new` and `delete` can be overloaded, but their return types are fixed. Particularly, the return types cannot be lambda functors, which prevents them to be overloaded for lambda expressions. It is not possible to take the address of a constructor, hence constructors cannot be used as target functions in bind expressions. The same is true for destructors. As a way around these constraints, BLL defines wrapper classes for `new` and `delete` calls, as well as for constructors and destructors. Instances of these classes are function objects, that can be used as target functions of bind expressions. For example:

    int* a[10];
    for_each(a, a+10, _1 = bind(new_ptr<int>()));
    for_each(a, a+10, bind(delete_ptr(), _1));

`new_ptr<int>()` ¤È¤¤¤¦¼°¤Ï¡¢ ¸Æ½Ð¤µ¤ì¤¿¤È¤¤Ë¡¢`new int()`¤ò¸Æ½Ð¤¹´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤òÀ¸À®¤¹¤ë¡£ ¤½¤·¤Æ¡¢`bind` Ãæ¤Ë¤ª¤¤¤Æ¤½¤Î¸Æ½Ð¤·¤ò¥é¥Ã¥×¤¹¤ë¤³¤È¤Ë¤è¤ê¡¢ ¦Ë¥Õ¥¡¥ó¥¯¥¿¤È¤·¤Æ¤¤¤ë¡£ Æ±ÍÍ¤Ë¡¢`delete_ptr()` ¤È¤¤¤¦¼°¤Ï°ú¿ô¤ËÂÐ¤·¤Æ¡¢`delete`¤ò ¸Æ½Ð¤¹´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤òÀ¸À®¤¹¤ë¡£ `new_ptr<`T`>()` ¤âÆ±ÍÍ¤Ë°ú¿ô¤ò¼è¤ë¤³¤È¤¬¤Ç¤¤ë¡£ ¤½¤Î°ú¿ô¤ÏÄ¾ÀÜ¥³¥ó¥¹¥È¥é¥¯¥¿¤Î¸Æ½Ð¤·¤ËÅÏ¤µ¤ì¤ë¡£ ¤½¤·¤Æ¡¢¤½¤Î¤³¤È¤Ë¤è¤ê°ú¿ô¤ò¤È¤ë¥³¥ó¥¹¥È¥é¥¯¥¿¤Î¸Æ½Ð¤·¤¬²ÄÇ½¤Ë¤Ê¤ë¡£
The `new_ptr<int>()` expression creates a function object that calls `new int()` when invoked, and wrapping that inside `bind` makes it a lambda functor. In the same way, the expression `delete_ptr()` creates a function object that invokes `delete` on its argument. Note that `new_ptr<`T`>()` can take arguments as well. They are passed directly to the constructor invocation and thus allow calls to constructors which take arguments.

¦Ë¼°¤Ë¤è¤ë¥³¥ó¥¹¥È¥é¥¯¥¿¤ÎÎã¤ò¼¨¤¹¡£ °Ê²¼¤Î¥³¡¼¥É¤Ç¤Ï¡¢Æó¤Ä¤Î¥³¥ó¥Æ¥Ê `x` ¤È `y` ¤«¤éÀ°¿ô¤ò¼è¤ê½Ð¤·¡¢ ¤½¤ì¤é¤Î pair ¤ò¹½ÃÛ¤·¡¢»°¤ÄÌÜ¤Î¥³¥ó¥Æ¥Ê¤ØÁÞÆþ¤¹¤ë¡£

    vector<pair<int, int> > v;
    transform(x.begin(), x.end(), y.begin(), back_inserter(v),
              bind(constructor<pair<int, int> >(), _1, _2));

Table 1 ¤Ë¥ª¥Ö¥¸¥§¥¯¥È¤Î¹½ÃÛ¤ÈÇË´þ¤Ë´Ø¤¹¤ë¤¹¤Ù¤Æ¤Î´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤ò¤Þ¤È¤á¤¿¡£ ¤³¤ÎÉ½¤Ï¡¢´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤ÎÀ¸À®¤È¸Æ½Ð¤·¤Î¼°¤È¤³¤Î¼°¤ÎÉ¾²Á¤Î¸ú²Ì¤ò¼¨¤·¤Æ¤¤¤ë¡£

As an example of constructor calls in lambda expressions, the following code reads integers from two containers `x` and `y`, constructs pairs out of them and inserts them into a third container:

    vector<pair<int, int> > v;
    transform(x.begin(), x.end(), y.begin(), back_inserter(v),
              bind(constructor<pair<int, int> >(), _1, _2));

Table 1 lists all the function objects related to creating and destroying objects, showing the expression to create and call the function object, and the effect of evaluating that expression.

Table 1. Construction and destruction related function objects.

Function object call | Wrapped expression
--- | ---
`constructor<T>()(`arg_list`)` | T(`arg_list`)
`destructor()(a)` | `a.~A()`, where `a` is of type `A`
`destructor()(pa)` | `pa.->A()`, where `pa` is of type `A*`
`new_ptr<T>()(`arg_list`)` | `new T(`arg_list`)`
`new_array<T>()(sz)` | `new T[sz]`
`delete_ptr()(p)` | `delete p`
`delete_array()(p)` | `delete p[]`

### 5.9. Special lambda expressions

#### 5.9.1. Preventing argument substitution

¦Ë¥Õ¥¡¥ó¥¯¥¿¤¬¸Æ½Ð¤µ¤ì¤¿¤È¤¡¢¥Ç¥Õ¥©¥ë¥È¤ÎÆ°ºî¤Ç¤Ï¡¢¤¹¤Ù¤Æ¤ÎÉôÊ¬¼°¤Ë¤ª¤¤¤Æ¼Â°ú¿ô¤ò¥×¥ì¡¼¥¹¥Û¥ë¥À¤ÈÃÖ¤´¹¤¨¤ë¡£ ¤³¤Î¥µ¥Ö¥»¥¯¥·¥ç¥ó¤Ç¤Ï¡¢ÃÖ¤´¹¤¨¤ÈÉôÊ¬¼°¤ÎÉ½¤òËÉ¤°ÊýË¡¤ò¼¨¤·¡¢ ¤³¤Îµ¡Ç½¤¬É¬Í×¤Ê¾ì¹ç¤Ë´Ø¤·¤ÆÀâÌÀ¤¹¤ë¡£

When a lambda functor is called, the default behavior is to substitute the actual arguments for the placeholders within all subexpressions. This section describes the tools to prevent the substitution and evaluation of a subexpression, and explains when these tools should be used.

bind ¼°¤Î°ú¿ô¤Ë¤ÏÇ¤°Õ¤Î¦Ë¼°¤¬¤Ê¤ë¤³¤È¤¬¤¢¤ê¤¨¤ë¡£ Îã¤¨¤Ð¡¢¼¡¤Î¤è¤¦¤ËÊÌ¤Ê bind ¼°¤¬°ú¿ô¤È¤Ê¤ë¤³¤È¤â²ÄÇ½¤Ç¤¢¤ë¡£

    int foo(int); int bar(int);
    ...
    int i;
    bind(foo, bind(bar, _1)(i);

The arguments to a bind expression can be arbitrary lambda expressions, e.g., other bind expressions. For example:

    int foo(int); int bar(int);
    ...
    int i;
    bind(foo, bind(bar, _1)(i);

ºÇ¸å¤Î¹Ô¤Ç¤Ï¡¢`foo(bar(i));` ¤¬¸Æ½Ð¤µ¤ì¤ë¡£ bind ¼°¤ÎºÇ½é¤Î°ú¿ô¤Ï¡¢¤Ä¤Þ¤êÂÐ¾Ý¤Î´Ø¿ô¤Ï¡¢Îã³°¤¬¤Ê¤¯¡¢ bind ¼°¤â¤³¤Î°ú¿ô¤È¤Ê¤ê¤¦¤ë¡£ °ìÈÖÆâÂ¦¤Î¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ï¡¢ÊÌ¤Ê¦Ë¥Õ¥¡¥ó¥¯¥¿¡¢´Ø¿ô¥Ý¥¤¥ó¥¿¡¢¥á¥ó¥Ð´Ø¿ô¤Ø¤Î¥Ý¥¤¥ó¥¿¤È¤¤¤Ã¤¿¡¢ÂÐ¾Ý¤Î´Ø¿ô¤È¤·¤Æ»ÈÍÑ¤µ¤ì¤ë¤â¤Î¤òÃ±¤ËÊÖ¤»¤Ð¤è¤¤¡£ Îã¤¨¤Ð¼¡¤Î¥³¡¼¥É¤Ç¤Ï¡¢°ìÈÖÆâÂ¦¤Î¦Ë¥Õ¥¡¥ó¥¯¥¿¤ÏÆó¤Ä¤Î´Ø¿ô¤ÎÁªÂò¤ò¤·¡¢ ¤½¤Î¤¦¤Á¤Î°ì¤Ä¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£

    int add(int a, int b) { return a+b; }
    int mul(int a, int b) { return a*b; }

    int(*)(int, int)  add_or_mul(bool x) {
      return x ? add : mul;
    }

    bool condition; int i; int j;
    ...
    bind(bind(&add_or_mul, _1), _2, _3)(condition, i, j);

The last line makes the call `foo(bar(i));` Note that the first argument in a bind expression, the target function, is no exception, and can thus be a bind expression too. The innermost lambda functor just has to return something that can be used as a target function: another lambda functor, function pointer, pointer to member function etc. For example, in the following code the innermost lambda functor makes a selection between two functions, and returns a pointer to one of them:

    int add(int a, int b) { return a+b; }
    int mul(int a, int b) { return a*b; }

    int(*)(int, int)  add_or_mul(bool x) {
      return x ? add : mul;
    }

    bool condition; int i; int j;
    ...
    bind(bind(&add_or_mul, _1), _2, _3)(condition, i, j);

##### 5.9.1.1. Unlambda

ÂÐ¾Ý¤Î´Ø¿ô¤¬¥Æ¥ó¥×¥ì¡¼¥È°ú¿ô¤Ë°ÍÂ¸¤¹¤ë·¿¤¬²ÄÊÑ¤Ç¤¢¤ë¾ì¹ç¤Ë¤Ï¡¢ ¤¤¤Ä¤Î¤Þ¤Ë¤« bind ¼°¤¬¥Í¥¹¥È¤·¤Æ¤·¤Þ¤¦¤³¤È¤¬¤¢¤ë¡£ °ìÈÌÅª¤ËÂÐ¾Ý¤Î´Ø¿ô¤Ï´Ø¿ô¥Æ¥ó¥×¥ì¡¼¥È¤Î·Á¼°Åª°ú¿ô¤È¤Ê¤ê¤¦¤ë¡£ ¤³¤Î¤è¤¦¤Ê¾ì¹ç¤Ë¤Ï¡¢¥×¥í¥°¥é¥Þ¤ÏÂÐ¾Ý¤Î´Ø¿ô¤¬¦Ë¥Õ¥¡¥ó¥¯¥¿¤«¤É¤¦¤«ÃÎ¤é¤Ê¤¤¤«¤â¤·¤ì¤Ê¤¤¡£

A nested bind expression may occur inadvertently, if the target function is a variable with a type that depends on a template parameter. Typically the target function could be a formal parameter of a function template. In such a case, the programmer may not know whether the target function is a lambda functor or not.

¼¡¤Î´Ø¿ô¥Æ¥ó¥×¥ì¡¼¥È¤ò¹Í¤¨¤Æ¤ß¤ë¡£

    template<class F>
    int nested(const F& f) {
      int x;
      ...
      bind(f, _1)(x);
      ...
    }

Consider the following function template:

    template<class F>
    int nested(const F& f) {
      int x;
      ...
      bind(f, _1)(x);
      ...
    }

¤³¤Î´Ø¿ôÆâ¤Ç¤Ï¡¢·Á¼°Åª°ú¿ô `f` ¤Ï bind ¼°¤ÎÂÐ¾Ý¤Î´Ø¿ô¤È¤·¤ÆÍøÍÑ¤µ¤ì¤Æ¤¤¤ë¡£ ¤³¤Î `bind` ¤Î¸Æ½Ð¤·¤¬Í¸ú¤Ç¤¢¤ë¤¿¤á¤Ë¤Ï¡¢ `f` ¤ÏÃ±¹à´Ø¿ô¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£ °Ê²¼¤ÎÆó¤Ä¤Î `nested` ¤Î¸Æ½Ð¤·¤ò¹Í¤¨¤Æ¤ß¤ë¡£

    int foo(int);
    int bar(int, int);
    nested(&foo);
    nested(bind(bar, 1, _1));

Somewhere inside the function the formal parameter `f` is used as a target function in a bind expression. In order for this `bind` call to be valid, `f` must be a unary function. Suppose the following two calls to `nested` are made:

    int foo(int);
    int bar(int, int);
    nested(&foo);
    nested(bind(bar, 1, _1));

Î¾Êý¤È¤âÃ±¹à¤Î´Ø¿ô¤Þ¤¿¤Ï´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤Ç¤¢¤ê¡¢Å¬ÀÚ¤Ê°ú¿ô¤ÈÊÖ¤êÃÍ¤ò¤â¤Ä¡£ ¤·¤«¤·¡¢¸å¼Ô¤Ï¥³¥ó¥Ñ¥¤¥ë¤Ç¤¤Ê¤¤¡£ ¸å¼Ô¤Î¸Æ½Ð¤·¤Ë¤ª¤¤¤Æ¤Ï¡¢`nested` ¤ÎÆâÉô¤Î bind ¼°¤Ï¼¡¤Î¤è¤¦¤Ë¤Ê¤Ã¤Æ¤·¤Þ¤¦¡£

    bind(bind(bar, 1, _1), _1)

Both are unary functions, or function objects, with appropriate argument and return types, but the latter will not compile. In the latter call, the bind expression inside `nested` will become:

    bind(bind(bar, 1, _1), _1)

¤³¤ì¤¬¡¢°ú¿ô `x` ¤ò¤È¤Ã¤Æ¸Æ½Ð¤µ¤ì¤¿¤È¤¡¢ ÃÖ´¹¤µ¤ì¡¢·ë¶É¤Ï¼¡¤Î¤è¤¦¤Ë¸Æ½Ð¤¹¤³¤È¤È¤Ê¤ë¡£

    bar(1, x)(x)

When this is invoked with `x`, after substituitions we end up trying to call

    bar(1, x)(x)

¤³¤ì¤Ï¡¢¥¨¥é¡¼¤È¤Ê¤ë¡£ `bar` ¤Î¸Æ½Ð¤·¤ÎÊÖ¤êÃÍ¤Ï int ¤Ç¤¢¤ê¡¢ Ã±¹à¤Î´Ø¿ô¤ä´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤Ç¤Ï¤Ê¤¤¡£
which is an error. The call to `bar` returns int, not a unary function or function object.

¾åµ¤ÎÎã¤Ç¤Ï¡¢`nested` Ãæ¤Î bind ¼°¤Î°Õ¿Þ¤Ï `f` ¤ò ¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ç¤Ï¤Ê¤¯¡¢ÄÌ¾ï¤Î´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤È¤·¤Æ°·¤¦¤³¤È¤Ç¤¢¤ë¡£ BLL ¤Ï´Ø¿ô¥Æ¥ó¥×¥ì¡¼¥È `unlambda` ¤òÄó¶¡¤·¤Æ¤¤¤ë¡£ ¤³¤Î´Ø¿ô¥Æ¥ó¥×¥ì¡¼¥È¤Ë¤è¤ê¡¢ `unlambda` ¤ÎÆâÉô¤Ë¥é¥Ã¥×¤µ¤ì¤¿¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ï¤â¤Ï¤ä¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ç¤Ï¤Ê¤¯¡¢°ú¿ô¤ÎÂåÆþ¤Î¥×¥í¥»¥¹¤Ë¤Ï´Ø·¸¤·¤Ê¤¤¡£ ¤È¤¤¤¦¤³¤È¤òÉ½¸½¤¹¤ë¡£ ¦Ë¥Õ¥¡¥ó¥¯¥¿°Ê³°¤Î·¿¤Î°ú¿ô¤ËÂÐ¤·¤Æ¤Ï¡¢`unlambda` ¤Ï const ¤Ê¥ª¥Ö¥¸¥§¥¯¥È¤òÈó const ¤ËÊÑ¹¹¤¹¤ë¤³¤È¤ò½ü¤¤¤Æ¡¢ ¹±Åù±é»» ¤Ç¤¢¤ë¡£

In the example above, the intent of the bind expression in the `nested` function is to treat `f` as an ordinary function object, instead of a lambda functor. The BLL provides the function template `unlambda` to express this: a lambda functor wrapped inside `unlambda` is not a lambda functor anymore, and does not take part into the argument substitution process. Note that for all other argument types `unlambda` is an identity operation, except for making non-const objects const.

`unlambda` ¤òÍøÍÑ¤·¤Æ¡¢ ´Ø¿ô `nested` ¤Ï¼¡¤Î¤è¤¦¤Ë½ñ¤¯¤³¤È¤¬¤Ç¤¤ë¡£

    template<class F>
    int nested(const F& f) {
      int x;
      ...
      bind(unlambda(f), _1)(x);
      ...
    }

Using `unlambda`, the `nested` function is written as:

    template<class F>
    int nested(const F& f) {
      int x;
      ...
      bind(unlambda(f), _1)(x);
      ...
    }

##### 5.9.1.2. Protect

´Ø¿ô `protect` ¤Ï unlambda ¤È´Ø·¸¤¬¤¢¤ë¡£ ¤³¤Î´Ø¿ô¤â°ú¿ô¤ÎÂåÆþ¤òËÉ¤°¤¿¤á¤ËÍøÍÑ¤µ¤ì¤ë¡£ ¤·¤«¤·¡¢`unlambda` ¤Ï¦Ë¥Õ¥¡¥ó¥¯¥¿¤ò±ÊÂ³Åª¤ËÄÌ¾ï¤Î´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤ËÊÑ´¹¤¹¤ë¤Î¤ËÂÐ¤·¤Æ¡¢`protected` ¤Ï¤³¤ÎÁàºî¤ò°ì²ó¤ÎÉ¾²Á¤À¤±¤Ë°ì»þÅª¤Ë¹Ô¤¦¡£ ¼¡¤ËÎã¤ò¼¨¤¹¡£

    int x = 1, y = 10;
    (_1 + protect(_1 + 2))(x)(y);

ºÇ½é¤Î¸Æ½Ð¤·¤Ç¤Ï¡¢`x` ¤òºÇº¸¤Î `_1` ¤ËÂåÆþ¤µ¤ì¡¢ ¤½¤Î·ë²Ì¤ÏÊÌ¤Ê¦Ë¥Õ¥¡¥ó¥¯¥¿ `x + (_1 + 2)` ¤È¤Ê¤ë¡£ ¤½¤Î¦Ë¥Õ¥¡¥ó¥¯¥¿¤¬ `y` ¤È¤È¤â¤Ë¸Æ½Ð¤µ¤ì¤ë¤È¡¢ `x + (y + 2)` ¤È¤Ê¤ê¡¢ºÇ½ªÅª¤Ë¤Ï 13 ¤È¤Ê¤ë¡£

The `protect` function is related to unlambda. It is also used to prevent the argument substitution taking place, but whereas `unlambda` turns a lambda functor into an ordinary function object for good, `protect` does this temporarily, for just one evaluation round. For example:

    int x = 1, y = 10;
    (_1 + protect(_1 + 2))(x)(y);

The first call substitutes `x` for the leftmost `_1`, and results in another lambda functor `x + (_1 + 2)`, which after the call with `y` becomes `x + (y + 2)`, and thus finally 13.

`protected` ¤ò¥é¥¤¥Ö¥é¥ê¤Ë´Þ¤á¤¿¼çÍ×¤ÊÆ°µ¡¤Ï¥Í¥¹¥È¤·¤¿ STL ¥¢¥ë¥´¥ê¥º¥à¤Î¸Æ½Ð¤·¤ò²ÄÇ½¤Ë¤¹¤ë¤³¤È¤Ç¤¢¤Ã¤¿¡£ (Section 5.11)

Primary motivation for including `protect` into the library, was to allow nested STL algorithm invocations (Section 5.11).

#### 5.9.2. Rvalues as actual arguments to lambda functors

Èó const ¤Ê±¦ÊÕÃÍ¤¬¦Ë¥Õ¥¡¥ó¥¯¥¿¤Î¼Â°ú¿ô¤È¤Ê¤ë¤³¤È¤Ï¤Ç¤¤Ê¤¤¡£ ¤³¤ì¤Ï°Õ¿Þ¤·¤Æ·èÄê¤µ¤ì¤¿Àß·×¤Î¤¿¤á¤Ç¤¢¤ë¡£ ¤³¤ÎÀ©¸Â¤¬¤Ê¤±¤ì¤Ð¡¢¼Â°ú¿ô¤ËÂÐ¤¹¤ëÉûºîÍÑ¤òÍ¿¤¨¤ë¤³¤È¤¬¤Ç¤¤Ê¤¯¤Ê¤ë¡£ ¤³¤ÎÀ©¸Â¤ò²óÈò¤¹¤ëÊýË¡¤¬¤¢¤ë¡£ ¥µ¥Ö¥»¥¯¥·¥ç¥ó Section 4.3 ¤ÎÎã¤òºÆ¤Ó»ÈÍÑ¤·¤Æ¡¢¤¤¤¯¤Ä¤«¤Î²ò·èºö¤òÊÂ¤Ù¤ë¡£

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

Actual arguments to the lambda functors cannot be non-const rvalues. This is due to a deliberate design decision: either we have this restriction, or there can be no side-effects to the actual arguments. There are ways around this limitation. We repeat the example from section Section 4.3 and list the different solutions:

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

1. ¤â¤·¡¢±¦ÊÕÃÍ¤¬¥¯¥é¥¹·¿¤Ç¤¢¤ì¤Ð¡¢±¦ÊÕÃÍ¤òºîÀ®¤¹¤ë´Ø¿ô¤ÎÊÖ¤êÃÍ¤Î·¿¤Ï const ¤È¤·¤ÆÄêµÁ¤µ¤ì¤ë¤Ù¤¤Ç¤¢¤ë¡£ ÉÔ¹¬¤Ë¤â¸À¸ì»ÅÍÍ¤ÎÀ©¸Â¤Ë¤è¤ê¡¢¤³¤ÎÊýË¡¤ÏÁÈ¤ß¹þ¤ß¤Î·¿¤ËÂÐ¤·¤Æ¤Ï¤¦¤Þ¤¯¤¤¤«¤Ê¤¤¡£ ÁÈ¤ß¹þ¤ß¤Î·¿¤Î±¦ÊÕÃÍ¤Ï¡¢const ½¤¾þ¤¹¤ë¤³¤È¤¬¤Ç¤¤Ê¤¤¤¿¤á¤Ç¤¢¤ë¡£

   If the rvalue is of a class type, the return type of the function that creates the rvalue should be defined as const. Due to an unfortunate language restriction this does not work for built-in types, as built-in rvalues cannot be const qualified.

2. ¦Ë´Ø¿ô¤Î¸Æ½Ð¤·¤ÎÉôÊ¬¤òÊÑ¹¹¤Ç¤¤ë¤Î¤Ç¤¢¤ì¤Ð¡¢ ´Ø¿ô `make_const` ¤òÍøÍÑ¤·¤Æ±¦ÊÕÃÍ¤ò const ½¤¾þ¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£ ¼¡¤ËÎã¤ò¼¨¤¹¡£

       (_1 + _2)(make_const(1), make_const(2)); // ok

   °ìÈÌÅª¤Ë¦Ë´Ø¿ô¤Î¸Æ½Ð¤·¤ÏÉ¸½à¥¢¥ë¥´¥ê¥º¥à¤Î¥Æ¥ó¥×¥ì¡¼¥È´Ø¿ô¤ÎÆâÉô¤Ç¹Ô¤ï¤ì¤ë¤¿¤á¡¢¤³¤Î²ò·èºö¤ò»ÅÍÍ¤¹¤ë¤³¤È¤ÏÆñ¤·¤¤¡£

   If the lambda function call is accessible, the `make_const` function can be used to constify the rvalue. E.g.:

       (_1 + _2)(make_const(1), make_const(2)); // ok

   Commonly the lambda function call site is inside a standard algorithm function template, preventing this solution to be used.

3. ¾åµ¤Î¤¤¤º¤ì¤â¤¬ÉÔ²ÄÇ½¤Ç¤¢¤ì¤Ð¡¢¦Ë¼°¤ò ´Ø¿ô`const_parameters` ¤Ç ¥é¥Ã¥×¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£ ¤³¤Î´Ø¿ô¤Ï°ú¿ô¤ò const »²¾È¤È¤·¤Æ¤È¤ëÊÌ¤Î·¿¤Î¦Ë¥Õ¥¡¥ó¥¯¥¿¤òÀ¸À®¤¹¤ë¡£ °Ê²¼¤ËÎã¤ò¼¨¤¹¡£

       const_parameters(_1 + _2)(1, 2); // ok

   `const_parameters` ¤ÏÁ´¤Æ¤Î°ú¿ô¤ò const ¤Ê·¿¤Ë¤¹¤ë¡£ ¤½¤Î¤¿¤á¡¢°ú¿ô¤ËÈó const ¤Ê±¦ÊÕÃÍ¤¬¤¢¤Ã¤¿¤ê¡¢Èó const »²¾È¤È¤·¤ÆÅÏ¤·¤¿¤¤°ú¿ô¤¬¤¢¤Ã¤¿¾ì¹ç¤Ë¤Ï¤³¤ÎÊýË¡¤Ï»ÈÍÑ¤Ç¤¤Ê¤¤¡£

   If neither of the above is possible, the lambda expression can be wrapped in a `const_parameters` function. It creates another type of lambda functor, which takes its arguments as const references. For example:

       const_parameters(_1 + _2)(1, 2); // ok

   Note that `const_parameters` makes all arguments const. Hence, in the case were one of the arguments is a non-const rvalue, and another argument needs to be passed as a non-const reference, this approach cannot be used.

4. °Ê¾å¤Î¤¤¤Å¤ì¤âÉÔ²ÄÇ½¤Ç¤¢¤ë¾ì¹ç¤Ë¤â¡¢»ÄÇ°¤Ê¤¬¤é const À¤òÇË¤ë¤³¤È¤â²ÄÇ½¤È¤Ê¤Ã¤Æ¤·¤Þ¤¦¤¬¡¢°ì¤Ä¤Î²ò·èºö¤¬Â¸ºß¤¹¤ë¡£ ¤³¤Î²ò·èË¡¤Ï¤µ¤é¤ËÊÌ¤Ê¦Ë¥Õ¥¡¥ó¥¯¥¿¤Î¥é¥Ã¥Ñ¤ò»ÈÍÑ¤¹¤ë¤³¤È¤Ç¤¢¤ë¡£ ¤³¤Î¥é¥Ã¥Ñ¤Ë¤Ï `break_const` ¤ÈÌ¾ÉÕ¤±¤Æ¡¢¥æ¡¼¥¶¤Ë¤³¤Î´Ø¿ô¤ÎÀøºßÅª¤Ê´í¸±¤ËÃí°Õ¤òÂ¥¤·¤Æ¤¤¤ë¡£ ´Ø¿ô `break_const` ¤Ï const ¤È¤·¤Æ°ú¿ô¤ò¼õ¤±¼è¤ê¡¢¥é¥Ã¥×¤µ¤ì¤¿¦Ë¥Õ¥¡¥ó¥¯¥¿¤ò¸Æ½Ð¤¹Á°¤Ë const ¤ò¼Î¤Æµî¤ë¦Ë¥Õ¥¡¥ó¥¯¥¿¤òÀ¸À®¤¹¤ë¡£ ¼¡¤ËÎã¤ò¼¨¤¹¡£ For example:

       int i;
       ...
       (_1 += _2)(i, 2);                 // error, 2 is a non-const rvalue
       const_parameters(_1 += _2)(i, 2); // error, i becomes const
       break_const(_1 += _2)(i, 2);      // ok, but dangerous

   If none of the above is possible, there is still one solution, which unfortunately can break const correctness. The solution is yet another lambda functor wrapper, which we have named `break_const` to alert the user of the potential dangers of this function. The `break_const` function creates a lambda functor that takes its arguments as const, and casts away constness prior to the call to the original wrapped lambda functor. For example:

       int i;
       ...
       (_1 += _2)(i, 2);                 // error, 2 is a non-const rvalue
       const_parameters(_1 += _2)(i, 2); // error, i becomes const
       break_const(_1 += _2)(i, 2);      // ok, but dangerous

   `break_const` ¤ä `const_parameters` ¤Î·ë²Ì¤Ï¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ç¤Ï¤Ê¤¤¡£¤½¤Î¤¿¤á¡¢Îã¤¨¤Ð¼¡¤Î¤è¤¦¤Ë¡¢¦Ë¼°¤ÎÉôÊ¬¼°¤È¤·¤Æ»ÈÍÑ¤¹¤ë¤³¤È¤Ï¤Ç¤¤Ê¤¤¡£

       break_const(_1 + _2) + _3; // fails.
       const_parameters(_1 + _2) + _3; // fails.

   Note, that the results of `break_const` or `const_parameters` are not lambda functors, so they cannot be used as subexpressions of lambda expressions. For instance:

       break_const(_1 + _2) + _3; // fails.
       const_parameters(_1 + _2) + _3; // fails.

   ¤·¤«¤·¡¢ÉôÊ¬Åª¤Ê¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ï BLL ¤ÎÆâÉô¤Ç¸Æ½Ð¤µ¤ì¡¢Èó const ¤Ê±¦ÊÕÃÍ¤ÎÌäÂê¤Î±Æ¶Á¤ò¼õ¤±¤Ê¤¤¤¿¤á¡¢¤³¤Î¼ï¤Î¥³¡¼¥É¤Ï·è¤·¤ÆÉ¬Í×¤È¤µ¤ì¤Ê¤¤¡£
   However, this kind of code should never be necessary, since calls to sub lambda functors are made inside the BLL, and are not affected by the non-const rvalue problem.

### 5.10. Casts, sizeof and typeid

#### 5.10.1. Cast expressions

BLL ¤Ï `static_cast`¡¢`dynamic_casy`¡¢ `const_cast`¡¢`reinterpret_cast` ¤Î »Í¤Ä¤Î¥¥ã¥¹¥È¤ÈÆ±Åù¤Ê¤â¤Î¤òÄêµÁ¤·¤Æ¤¤¤ë¡£ BLL ¤Î¥¥ã¥¹¥È¤Ë¤Ï¡¢`ll_` ¤È¤¤¤¦ÀÜÆ¬¼¤¬ÉÕ¤¤¤Æ¤¤¤ë¡£ ¥¥ã¥¹¥ÈÀè¤Î·¿¤Ï¥Æ¥ó¥×¥ì¡¼¥È°ú¿ô¤È¤·¤ÆÌÀ¼¨Åª¤Ë»ØÄê¤·¡¢ Í£°ì¤Î°ú¿ô¤Ï¥¥ã¥¹¥È¤ò¹Ô¤Ê¤¦¼°¤Ç¤¢¤ë¡£ °ú¿ô¤¬¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ç¤¢¤ì¤Ð¡¢¦Ë¥Õ¥¡¥ó¥¯¥¿¤¬¤Þ¤ºÉ¾²Á¤µ¤ì¤ë¡£ Îã¤¨¤Ð¡¢°Ê²¼¤Î¥³¡¼¥É¤Ç¤Ï¡¢`ll_dynamic_cast` ¤ò»ÈÍÑ¤·¤Æ¡¢ ¥³¥ó¥Æ¥Ê `a` Æâ¤Î `derived` ·¿¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤Î¿ô¤ò¿ô¤¨¤ë¡£

    class base {};
    class derived : public base {};

    vector<base*> a;
    ...
    int count = 0;
    for_each(a.begin(), a.end(),
             if_then(ll_dynamic_cast<derived*>(_1), ++var(count)));

The BLL defines its counterparts for the four cast expressions `static_cast`, `dynamic_cast`, `const_cast` and `reinterpret_cast`. The BLL versions of the cast expressions have the prefix `ll_`. The type to cast to is given as an explicitly specified template argument, and the sole argument is the expression from which to perform the cast. If the argument is a lambda functor, the lambda functor is evaluated first. For example, the following code uses `ll_dynamic_cast` to count the number of `derived` instances in the container `a`:

    class base {};
    class derived : public base {};

    vector<base*> a;
    ...
    int count = 0;
    for_each(a.begin(), a.end(),
             if_then(ll_dynamic_cast<derived*>(_1), ++var(count)));

#### 5.10.2. Sizeof and typeid

BLL ¤Î sizeof ¤È typeid ¤ÈÆ±Åù¤Ê¤â¤Î¤Ï¡¢ `ll_sizeof` ¤È `ll_typeid` ¤ÈÌ¾ÉÕ¤±¤é¤Æ¤¤¤ë¡£ ÁÐÊý¤È¤â°ì¤Ä¤Î°ú¿ô¤ò¤È¤ë¡£¤½¤Î°ú¿ô¤Ï¦Ë¼°¤Ç¤â¤è¤¤¡£ ¤³¤ì¤é¤Ë¤è¤êºî¤é¤ì¤¿¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ï¡¢`sizeof` ¤ä `typeid` ¤Î¸Æ½Ð¤·¤ò¥é¥Ã¥×¤¹¤ë¡£¤½¤·¤Æ¡¢¦Ë¥Õ¥¡¥ó¥¯¥¿¤¬¸Æ½Ð¤µ¤ì¤¿¤È¤¤Ë¡¢ ¥é¥Ã¥×¤µ¤ì¤¿Áàºî¤¬¼Â¹Ô¤µ¤ì¤ë¡£ °Ê²¼¤ËÎã¤ò¼¨¤¹¡£

    vector<base*> a;
    ...
    for_each(a.begin(), a.end(),
             cout << bind(&type_info::name, ll_typeid(*_1)));

`ll_typeid` ¤Ï³ÆÍ×ÁÇ¤ËÂÐ¤·¤Æ¡¢`typeid` ¤ò¸Æ½Ð¤¹¦Ë¥Õ¥¡¥ó¥¯¥¿¤òÀ¸À®¤¹¤ë¡£ `typeid` ¤Î¸Æ½Ð¤·¤Î·ë²Ì¤Ï `type_info` ·¿¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤Ç¤¢¤ê¡¢ bind ¼°¤Ï¤½¤Î¥á¥ó¥Ð´Ø¿ô `name` ¤ò¸Æ¤Ö¦Ë¥Õ¥¡¥ó¥¯¥¿¤òÀ¸À®¤¹¤ë¡£

The BLL counterparts for these expressions are named `ll_sizeof` and `ll_typeid`. Both take one argument, which can be a lambda expression. The lambda functor created wraps the `sizeof` or `typeid` call, and when the lambda functor is called the wrapped operation is performed. For example:

    vector<base*> a;
    ...
    for_each(a.begin(), a.end(),
             cout << bind(&type_info::name, ll_typeid(*_1)));

Here `ll_typeid` creates a lambda functor for calling `typeid` for each element. The result of a `typeid` call is an instance of the `type_info` class, and the bind expression creates a lambda functor for calling the `name` member function of that class.

### 5.11. Nesting STL algorithm invocations

BLL ¤Ï¶¦ÄÌ¤Î STL ¥¢¥ë¥´¥ê¥º¥à¤ò´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¥¯¥é¥¹¤È¤·¤ÆÄêµÁ¤·¤Æ¤ª¤ê¡¢ ¤½¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤Ï bind ¼°¤ÎÂÐ¾Ý¤Î´Ø¿ô¤È¤·¤Æ»ÈÍÑ¤Ç¤¤ë¡£ Îã¤¨¤Ð¡¢°Ê²¼¤Î¥³¡¼¥É¤Ç¤Ï¡¢Æó¼¡¸µÇÛÎó¤ÎÍ×ÁÇ¤òÈ¿Éü¤·¤Æ¡¢¤½¤ÎÏÂ¤ò·×»»¤¹¤ë¡£

    int a[100][200];
    int sum = 0;

    std::for_each(a, a + 100,
    	      bind(ll::for_each(), _1, _1 + 200, protect(sum += _1)));

BLL ¤Î STL ¥¢¥ë¥´¥ê¥º¥à¤Ï¥¯¥é¥¹¤Ç¤¢¤ë¡£ ¤½¤Î¥¯¥é¥¹¤Ç¤Ï¡¢´Ø¿ô¸Æ½Ð¤·±é»»»Ò(»þ¤Ë¤Ï¥ª¡¼¥Ð¡¼¥í¡¼¥É¤·¤Æ) ¤òÄêµÁ¤·¡¢ `std` Ì¾Á°¶õ´Ö¤ÎÂÐ±þ¤¹¤¦´Ø¿ô¥Æ¥ó¥×¥ì¡¼¥È¤ò¸Æ½Ð¤¹¤è¤¦¤Ë¤·¤Æ¤¤¤ë¡£ ¤³¤ì¤é¤¹¤Ù¤Æ¤Î¹½Â¤ÂÎ¤ÏÉôÊ¬Ì¾Á°¶õ´Ö `boost::lambda::ll` ¤ËÂ¸ºß¤¹¤ë¡£

The BLL defines common STL algorithms as function object classes, instances of which can be used as target functions in bind expressions. For example, the following code iterates over the elements of a two-dimensional array, and computes their sum.

    int a[100][200];
    int sum = 0;

    std::for_each(a, a + 100,
    	      bind(ll::for_each(), _1, _1 + 200, protect(sum += _1)));

The BLL versions of the STL algorithms are classes, which define the function call operator (or several overloaded ones) to call the corresponding function templates in the `std` namespace. All these structs are placed in the subnamespace `boost::lambda:ll`.

¦Ë¼°¤Ë¤ª¤±¤ë¥ª¡¼¥Ð¡¼¥í¡¼¥É¤µ¤ì¤¿¥á¥ó¥Ð´Ø¿ô¤Î¸Æ½Ð¤·¤òÉ½¸½¤¹¤ë´ÊÃ±¤ÊÊýË¡¤Ï¤Ê¤¤¡£ ¤³¤Î¤³¤È¤Ë¤è¤ê¡¢¥Í¥¹¥È¤·¤¿ STL ¥¢¥ë¥´¥ê¥º¥à¤ÎÍ¸úÀ¤òÀ©¸Â¤µ¤ì¤Æ¤·¤Þ¤¦¡£ Îã¤¨¤Ð¡¢¥Æ¥ó¥×¥ì¡¼¥È¥³¥ó¥Æ¥Ê¤Ë¤ª¤¤¤Æ¡¢´Ø¿ô `begin` ¤Ï ¥ª¡¼¥Ð¡¼¥í¡¼¥É¤µ¤ì¤¿ÄêµÁ¤¬°ì¤Ä°Ê¾å¤¢¤ë¡£ °ìÈÌÅª¤Ë¤Ï¡¢°Ê²¼¤Îµ¼»÷¥³¡¼¥É¤ÈÎà»÷¤·¤¿¤â¤Î¤ò½ñ¤¯¤³¤È¤Ï¤Ç¤¤Ê¤¤¡£

    std::for_each(a.begin(), a.end(),
    	      bind(ll::for_each(), _1.begin(), _1.end(), protect(sum += _1)));

Note that there is no easy way to express an overloaded member function call in a lambda expression. This limits the usefulness of nested STL algorithms, as for instance the `begin` function has more than one overloaded definitions in container templates. In general, something analogous to the pseudo-code below cannot be written:

    std::for_each(a.begin(), a.end(),
    	      bind(ll::for_each(), _1.begin(), _1.end(), protect(sum += _1)));

¤·¤«¤·¡¢¶¦ÄÌ¤ÎÆÃ¼ì¤Ê¾ì¹ç¤Ë¤ÏÂÐ±þºö¤¬Äó¶¡¤µ¤ì¤Æ¤¤¤ë¡£ BLL ¤Ï `call_begin` ¤È `call_end` ¤È¤¤¤¦Æó¤Ä¤Î¥Ø¥ë¥Ñ´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¥¯¥é¥¹¤òÄêµÁ¤·¤Æ¤¤¤ë¡£ ¤½¤ì¤é¤Î¥¯¥é¥¹¤Ï¤½¤ì¤¾¤ì¥³¥ó¥Æ¥Ê¤Î´Ø¿ô `begin` ¤È´Ø¿ô `end` ¤Î¸Æ½Ð¤·¤ò¥é¥Ã¥×¤·¡¢¥³¥ó¥Æ¥Ê¤Î `const_iterator` ·¿¤òÊÖ¤¹¡£ ¤³¤ì¤é¤Î¥Ø¥ë¥Ñ¥Æ¥ó¥×¥ì¡¼¥È¤òÍÑ¤¤¤ë¤È¡¢¾åµ¤Î¥³¡¼¥É¤Ï¼¡¤Î¤è¤¦¤Ë¤Ê¤ë¡£

    std::for_each(a.begin(), a.end(),
    	      bind(ll::for_each(),
                       bind(call_begin(), _1), bind(call_end(), _1),
                            protect(sum += _1)));

Some aid for common special cases can be provided though. The BLL defines two helper function object classes, `call_begin` and `call_end`, which wrap a call to the `begin` and, respectively, `end` functions of a container, and return the `const_iterator` type of the container. With these helper templates, the above code becomes:

    std::for_each(a.begin(), a.end(),
    	      bind(ll::for_each(),
                       bind(call_begin(), _1), bind(call_end(), _1),
                            protect(sum += _1)));

PrevŽ | Up | Ž Next
4. Using the libraryŽ | Home | Ž 6. Extending return type deduction system