A. Rationale for some of the design decisions
PrevŽ | Ž | Ž Next

## A. Rationale for some of the design decisions

### 1. Lambda functor arity

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

    bind(g, _3, _3, _3)(x, y, z);
    bind(g, _1, _1, _1)(x, y, z);

The highest placeholder index in a lambda expression determines the arity of the resulting function object. However, this is just the minimal arity, as the function object can take arbitrarily many arguments; those not needed are discarded. Consider the two bind expressions and their invocations below:

    bind(g, _3, _3, _3)(x, y, z);
    bind(g, _1, _1, _1)(x, y, z);

ºÇ½é¤Î¹Ô¤Ç¤Ï¡¢°ú¿ô¤Î¤¦¤Á `x` ¤È `y` ¤òÌµ»ë¤·¤Æ¡¢ ¼¡¤Î¤è¤¦¤Ê¸Æ½Ð¤·¤È¤Ê¤ë¡£

    g(z, z, z)

This first line discards arguments `x` and `y`, and makes the call:

    g(z, z, z)

°ìÊý¡¢Æó¹ÔÌÜ¤Ç¤Ï¡¢°ú¿ô¤Î `y` ¤È `z` ¤¬Ìµ»ë¤µ¤ì¡¢ ¼¡¤Î¤è¤¦¤Ê¸Æ½Ð¤·¤È¤Ê¤ë¡£

    g(x, x, x)

½é´ü¤Î¥é¥¤¥Ö¥é¥ê¤Ç¤Ï¡¢¸å¼Ô¤Î·ë²Ì¤Ï¥³¥ó¥Ñ¥¤¥ë»þ¥¨¥é¡¼¤È¤Ê¤Ã¤¿¡£ ¤³¤ì¤Ï´ðËÜÅª¤Ë°ÂÁ´À¤È½ÀÆðÀ¤Î¥È¥ì¡¼¥É¥ª¥Õ¤Ç¤¢¤ê¡¢ ¤³¤ÎÌäÂê¤Ï¥é¥¤¥Ö¥é¥ê¤Î Boost ¤Î¥ì¥Ó¥å¡¼¤Î»þ´ü¤ËÂçµ¬ÌÏ¤ËÏÃ¤·¹ç¤ï¤ì¤¿¡£ ¸·Ì©¤Ê°ú¿ô¤Î¿ô ¥Á¥§¥Ã¥¯¤Î¼ç¤ÊÍøÅÀ¤Ï¡¢ ¥×¥í¥°¥é¥à¤Î¥¨¥é¡¼¤ò½é´ü¤ÎÃÊ³¬¤ÇÈ¯¸«¤Ç¤¤ë¤«¤â¤·¤ì¤Ê¤¤¤È¤¤¤¦¤³¤È¤È¡¢ ¼¡¤Î¤è¤¦¤ËÌÀ¼¨Åª¤Ë°ú¿ô¤òÌµ»ë¤¹¤ë¦Ë¼°¤Ï´ÊÃ±¤Ëµ½Ò¤Ç¤¤ë¤«¤é¤Ç¤¢¤ë¡£

    (_3, bind(g, _1, _1, _1))(x, y, z);

In earlier versions of the library, the latter line resulted in a compile time error. This is basically a tradeoff between safety and flexibility, and the issue was extensively discussed during the Boost review period of the library. The main points for the strict arity checking was that it might catch a programming error at an earlier time and that a lambda expression that explicitly discards its arguments is easy to write:

    (_3, bind(g, _1, _1, _1))(x, y, z);

¤³¤Î¦Ë¼°¤Ï»°¤Ä¤Î°ú¿ô¤ò¼è¤ë¡£ ¥«¥ó¥Þ±é»»»Ò¤Îº¸Â¦¤Î°ú¿ô¤Ï²¿¤â¤·¤Ê¤¯¡¢ ¥«¥ó¥Þ±é»»»Ò¤Ï±¦Â¦¤Î°ú¿ô¤ÎÉ¾²Á·ë²Ì¤òÊÖ¤¹¡£ °ú¿ô¤Î¿ô¤ò¸·Ì©¤Ë¥Á¥§¥Ã¥¯¤·¤¿¤È¤·¤Æ¤â¡¢ ·ë¶É¤Ï `g(x, x, x)` ¤Î¸Æ½Ð¤·¤Ë¤Ê¤ë¡£
This lambda expression takes three arguments. The left-hand argument of the comma operator does nothing, and as comma returns the result of evaluating the right-hand argument we end up with the call `g(x, x, x)` even with the strict arity.

¸·Ì©¤Ê°ú¿ô¤Î¿ô¤Î¥Á¥§¥Ã¥¯¤ËÈ¿ÂÐ¤¹¤ë¤³¤È¤Î¼ç¤ÊÍøÅÀ¤Ï¡¢ °ú¿ô¤òÌµ»ë¤¹¤ë¤³¤È¤¬É¬Í×¤È¤Ê¤ë¤Î¤ÏÉáÄÌ¤Ç¤¢¤ë¤Î¤Ç¡¢´ÊÃ±¤Ëµ½Ò¤Ç¤¤ë¤Ù¤¤Ç¤¢¤ë¤È¤¤¤¦¤³¤È¤È¡¢ ¼ÂºÝ¤Ë¤Ï¸·Ì©¤Ë°ú¿ô¤ò¥Á¥§¥Ã¥¯¤·¤Æ¤â¡¢ÂÐ¾ÝÅª¤Ê¦Ë¼°¤Î¾ì¹ç¤Ë¤ÏÆÃ¤Ë¡¢ ¼ÂºÝ¾å¤¢¤Þ¤ê°ÂÁ´À¤òÆÀ¤ë¤³¤È¤¬¤Ç¤¤Ê¤¤¤È¤¤¤¦¤³¤È¤Ç¤¢¤ë¡£ Îã¤¨¤Ð¡¢¥×¥í¥°¥é¥Þ¤¬ `_1 + _2` ¤È¤¤¤¦¼°¤òµ½Ò¤·¤¿¤«¤Ã¤¿¤¬ ¸í¤Ã¤Æ `_1 + 2` ¤Èµ½Ò¤·¤¿¾ì¹ç¤Ë¡¢ ¸·Ì©¤Ë°ú¿ô¤Î¿ô¤ò¥Á¥§¥Ã¥¯¤¹¤ì¤Ð¡¢¥³¥ó¥Ñ¥¤¥é¤Ï¥¨¥é¡¼¤òÈ¯¸«¤¹¤ë¡£ ¤·¤«¤·¡¢´Ö°ã¤Ã¤¿¼°¤¬ `1 + _2` ¤Ç¤¢¤Ã¤¿¾ì¹ç¤Ë¤Ï¡¢¥¨¥é¡¼¤ÏÈ¯¸«¤µ¤ì¤Ê¤¤¡£ ¤µ¤é¤Ë¡¢°ú¿ô¤ò´ËÏÂ¤·¤Æ¥Á¥§¥Ã¥¯¤¹¤ì¤Ð¡¢¼ÂÁõ¤¬¾¯¤·Ã±½ã¤Ë¤Ê¤ë¡£ Boost ¤Î¥ì¥Ó¥å¡¼¤Î¿äÁ¦¤Ë½¾¤Ã¤Æ¡¢¸·Ì©¤Ê°ú¿ô¤Î¥Á¥§¥Ã¥¯¤ÏµÑ²¼¤µ¤ì¤¿¡£

The main points against the strict arity checking were that the need to discard arguments is commonplace, and should therefore be straightforward, and that strict arity checking does not really buy that much more safety, particularly as it is not symmetric. For example, if the programmer wanted to write the expression `_1 + _2` but mistakenly wrote `_1 + 2`, with strict arity checking, the complier would spot the error. However, if the erroneous expression was `1 + _2` instead, the error would go unnoticed. Furthermore, weak arity checking simplifies the implementation a bit. Following the recommendation of the Boost review, strict arity checking was dropped.

PrevŽ | Up | Ž Next
9. ContributorsŽ | Home | Ž Bibliography