8. Relation to other Boost libraries
PrevŽ | Ž | Ž Next

## 8. Relation to other Boost libraries

### 8.1. Boost Function

¦Ë¥Õ¥¡¥ó¥¯¥¿¤òÊÑ¿ô¤Ë³ÊÇ¼¤¹¤ë¤ÈÅÔ¹ç¤Î¤¤¤¤¾ì¹ç¤¬¤¢¤ë¡£ ¤·¤«¤·¡¢ºÇ¤âÃ±½ã¤Ê¦Ë¥Õ¥¡¥ó¥¯¥¿¤Î·¿¤Ç¤¢¤Ã¤Æ¤â¡¢Ä¹¤¯¡¢¸½¼ÂÅª¤Ç¤Ê¤¤¡£ ¤½¤·¤Æ¡¢°ìÈÌÅª¤Ë¤Ï¦Ë¥Õ¥¡¥ó¥¯¥¿¤Î·¿¤ÎÊÑ¿ô¤òÀë¸À¤¹¤ë¤³¤È¤Ï¼Â¸½ÉÔ²ÄÇ½¤Ç¤¢¤ë¡£ Boost Function ¥é¥¤¥Ö¥é¥ê [function] ¤Ï¡¢Îã¤¨¤Ð¦Ë¥Õ¥¡¥ó¥¯¥¿¤Î¤è¤¦¤ÊÇ¤°Õ¤Î´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤Î¥é¥Ã¥Ñ¤òÄêµÁ¤¹¤ë¡£ ¤³¤ì¤é¤Î¥é¥Ã¥Ñ¤Ïµ½Ò¤·¤ä¤¹¤¤·¿¤È¤Ê¤Ã¤Æ¤¤¤ë¡£ °Ê²¼¤ËÎã¤ò¼¨¤¹¡£

    boost::function<int(int, int)> f = _1 + _2;
    boost::function<int&(int&)> g = (_1 += 10);
    int i = 1, j = 2;
    f(i); // returns 3
    g(i); // sets i to = 11;

Sometimes it is convenient to store lambda functors in variables. However, the types of even the simplest lambda functors are long and unwieldy, and it is in general unfeasible to declare variables with lambda functor types. The Boost Function library [function] defines wrappers for arbitrary function objects, for example lambda functors; and these wrappers have types that are easy to type out. For example:

    boost::function<int(int, int)> f = _1 + _2;
    boost::function<int&(int&)> g = (_1 += 10);
    int i = 1, j = 2;
    f(i); // returns 3
    g(i); // sets i to = 11;

¤¿¤È¤¨¡¢°ìÈÌÅª¤Ê°ú¿ô¤ò»ý¤Ã¤Æ¤¤¤ë¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ç¤¢¤Ã¤Æ¤â¡¢ ¥é¥Ã¥×¤¹¤ë´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤ÎÊÖ¤êÃÍ¤È°ú¿ô¤Î·¿¤Ï¡¢¥é¥Ã¥Ñ¥Æ¥ó¥×¥ì¡¼¥È `boost::function` ¤Î¥Æ¥ó¥×¥ì¡¼¥È°ú¿ô¤È¤·¤ÆÌÀ¼¨Åª¤Ëµ½Ò¤µ¤ì¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£ `boost::function` ¤Ç´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤ò¥é¥Ã¥×¤¹¤ë¤È¡¢¼ÂºÝ¤Ë¤Ï²¾ÁÛ´Ø¿ô¤ò»ÈÍÑ¤·¤Æ¤¤¤Ê¤¤¤¬¡¢²¾ÁÛ´Ø¿ô¤Î½èÍý¤ÈÆ±Åù¤Ê¥Ñ¥Õ¥©¡¼¥Þ¥ó¥¹¤Î¥³¥¹¥È¤¬À¸¤¸¤Æ¤·¤Þ¤¦¡£ `boost::function` Ãæ¤Ë¦Ë¥Õ¥¡¥ó¥¯¥¿¤ò³ÊÇ¼¤¹¤ë¤³¤È¤Ï´í¸±¤Î¸¶°ø¤È¤Ê¤ê¤¦¤ë¡£ ¤¢¤ë¼ï¤Î¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ï¡¢¦Ë¼°¤Î°ú¿ô¤Î¥³¥Ô¡¼¤ò¤È¤ëÂå¤ï¤ê¤Ë¡¢Â«ÇûÊÑ¿ô¤Ø¤Î»²¾È¤ò³ÊÇ¼¤·¤Æ¤¤¤ë¤«¤â¤·¤ì¤Ê¤¤¡£ SLT ¥¢¥ë¥´¥ê¥º¥à¤Î¸Æ½Ð¤·¤¬´°Î»¤·¤¿Ä¾¸å¤Ë¦Ë¥Õ¥¡¥ó¥¯¥¿¤ÏÇË´þ¤µ¤ì¤ë¤¿¤á¡¢STL ¥¢¥ë¥´¥ê¥º¥à¤Î¸Æ½Ð¤·¤Ç¡¢°ì»þÅª¤Ê¦Ë¥Õ¥¡¥ó¥¯¥¿¤Î¥ª¥Ö¥¸¥§¥¯¥È¤¬»ÈÍÑ¤µ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ï¾ï¤Ë°ÂÁ´¤Ç¤¢¤ë¡£ ¤·¤«¤·¡¢`boost::function` ¤ÎÆâÉô¤Ë¥é¥Ã¥×¤µ¤ì¤¿¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ï¡¢¤µ¤é¤ËÀ¸Â¸¤·Â³¤±¡¢Ìµ¸ú¤Ê»²¾È¤ò»ý¤Ä²ÄÇ½À¤òºî¤Ã¤Æ¤·¤Þ¤¦¡£ °Ê²¼¤Ë¡¢Îã¤ò¼¨¤¹¡£

    int* sum = new int();
    *sum = 0;
    boost::function<int&(int)> counter = *sum += _1;
    counter(5); // ok, *sum = 5;
    delete sum;
    counter(3); // error, *sum does not exist anymore

The return and parameter types of the wrapped function object must be written explicilty as the template argument to the wrapper template `boost::function`; even when lambda functors, which otherwise have generic parameters, are wrapped. Wrapping a function object with `boost::function` introduces a performance cost comparable to virtual function dispatch, though virtual functions are not actually used. Note that storing lambda functors inside `boost::function` introduces a danger. Certain types of lambda functors may store references to the bound arguments, instead as taking copies of the arguments of the lambda expression. When temporary lambda functor objects are used in STL algorithm invocations this is always safe, as the lambda functor gets destructed immediately after the STL algortihm invocation is completed. However, a lambda functor wrapped inside `boost::function` may continue to exist longer, creating the possibility of dangling references. For example:

    int* sum = new int();
    *sum = 0;
    boost::function<int&(int)> counter = *sum += _1;
    counter(5); // ok, *sum = 5;
    delete sum;
    counter(3); // error, *sum does not exist anymore

### 8.2. Boost Bind

Boost Bind [bind] ¥é¥¤¥Ö¥é¥ê¤ÏÉôÊ¬Åª¤Ë BLL ¤Îµ¡Ç½¤È½ÅÊ£¤¹¤ë¡£ ´ðËÜÅª¤Ë¤Ï¡¢Boost Bind ¥é¥¤¥Ö¥é¥ê(°Ê¹ß BB)¤Ç¤Ï¡¢BLL ¤Î°ìÉô¤Î bind ¼°¤ò¼ÂÁõ¤·¤Æ¤¤¤ë¤È¤¤¤¨¤ë¡£ ¤·¤«¤·¡¢¤¤¤¯¤Ä¤«°ÕÌ£Åª¤Ë°Û¤Ê¤ëÉôÊ¬¤â¤¢¤ë¡£

The Boost Bind [bind] library has partially overlapping functionality with the BLL. Basically, the Boost Bind library (BB in the sequel) implements the bind expression part of BLL. There are, however, some semantical differerences.

BLL ¤È BB ¤ÏÊÌ¡¹¤ËÈ¯Å¸¤·¡¢°Û¤Ã¤¿¼ÂÁõ¤È¤Ê¤Ã¤Æ¤¤¤ë¡£ ¤½¤Î¤¿¤á¡¢BB ¤Î bind ¼°¤Ï BLL ¤Î bind ¼°¡¢Â¾¤Î·Á¤Î¦Ë¼°¤Ë¤Ï»ÈÍÑ¤¹¤ë¤³¤È¤Ï¤Ç¤¤Ê¤¤¡£ BB ¤Ë¤ª¤¤¤Æ¡¢BLL ¤Î bind ¼°¤ò»ÈÍÑ¤¹¤ë¤Î¤Ë¤â¡¢Æ±¤¸À©¸Â¤¬¤¢¤ë¡£ ¤·¤«¤·¡¢BB ¥é¥¤¥Ö¥é¥ê¤ÎÌ¾Á°¤Ï¡¢Ì¾Á°¶õ´Ö `boost` ¤ËÂ¸ºß¤¹¤ë¤Î¤ËÂÐ¤·¡¢ BLL ¤ÎÌ¾Á°¤Ï¡¢Ì¾Á°¶õ´Ö `boost::lambda` ¤ËÂ¸ºß¤¹¤ë¤¿¤á¡¢ ¤³¤ì¤é¤ÎÆó¤Ä¤Î¥é¥¤¥Ö¥é¥ê¤Ï¶¦Â¸¤Ç¤¤ë¡£

The BLL and BB evolved separately, and have different implementations. This means that the bind expressions from the BB cannot be used within bind expressions, or within other type of lambda expressions, of the BLL. The same holds for using BLL bind expressions in the BB. The libraries can coexist, however, as the names of the BB library are in `boost` namespace, whereas the BLL names are in `boost::lambda` namespace.

BLL ¤Ï¤¢¤ëÄøÅÙ C++ É¸½à¤Ë½ç±þ¤Ê¥³¥ó¥Ñ¥¤¥é¤¬É¬Í×¤Ç¤¢¤ë¤Î¤ËÂÐ¤·¡¢ BB ¥é¥¤¥Ö¥é¥ê¤Ï¤è¤ê¥Ý¡¼¥¿¥Ö¥ë¤Ê¥³¥ó¥Ñ¥¤¥é¤Ç½½Ê¬¤Ç¤¢¤ê¡¢¤è¤êÂ¿¤¯¤Î¥³¥ó¥Ñ¥¤¥é¤Çµ¡Ç½¤¹¤ë¡£

The BLL requires a compiler that is reasonably conformant to the C++ standard, whereas the BB library is more portable, and works with a larger set of compilers.

°Ê¹ß¤ÎÆó¤Ä¤Î¥µ¥Ö¥»¥¯¥·¥ç¥ó¤Ç¤Ï¡¢ BB ¤È BLL ¤Ë¤ª¤±¤ë bind ¼°¤Î°ÕÌ£Åª¤ÊÁê°ã¤Ë¤Ä¤¤¤ÆÀâÌÀ¤¹¤ë¡£

The following two sections describe what are the semantic differences between the bind expressions in BB and BLL.

#### 8.2.1. First argument of bind expression

BB ¤Ë¤ª¤¤¤Æ¤Ï¡¢ÂÐ¾Ý¤Î´Ø¿ô¤Ç¤¢¤ë bind ¼°¤ÎÂè°ì°ú¿ô¤Î°·¤¤Êý¤Ï¡¢ Â¾¤Î°ú¿ô¤Î°·¤¤Êý¤È¤Ï°Û¤Ê¤ë¡£ ¤³¤Î°ú¿ô¤Ë´Ø¤·¤Æ¤Ï¡¢°ú¿ô¤ÎÂåÆþ¤¬¹Ô¤Ê¤ï¤ì¤Ê¤¤¤Î¤Ç¤¢¤ë¡£ BLL ¤Ë¤ª¤¤¤Æ¤Ï¡¢¤³¤ÎÅÀ¤Ë¤ª¤¤¤Æ¤ÏÂè°ì°ú¿ô¤ÏÆÃÊÌ°·¤¤¤Ï¤µ¤ì¤Ê¤¤¡£ °Ê²¼¤ËÎã¤ò¼¨¤¹¡£

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

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

In BB the first argument of the bind expression, the target function, is treated differently from the other arguments, as no argument substitution takes place within that argument. In BLL the first argument is not a special case in this respect. For example:

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

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

`foo` ¤ÎÆâÉô¤Î bind ¼°¤Ï¼¡¤Î¤è¤¦¤Ë¤Ê¤ë¡£

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

The bind expression inside `foo` becomes:

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

BLL ¤Ë¤ª¤¤¤Æ¤Ï¡¢¤³¤ì¤Ï¼¡¤Î¤è¤¦¤Ë²ò¼á¤µ¤ì¤ë¡£

    bar(1, x)(x)

The BLL interpretes this as:

    bar(1, x)(x)

°ìÊý¡¢BB ¤Ë¤ª¤¤¤Æ¤Ï¼¡¤Î¤è¤¦¤Ë¤Ê¤ë¡£

    bar(1, x)

whereas the BB library as

    bar(1, x)

BLL ¤Ç¤Ï¤³¤Îµ¡Ç½¤ò¼Â¸½¤¹¤ë¤¿¤á¡¢Section 5.9.1.1 ¤ÇÀâÌÀ¤·¤¿¤è¤¦¤Ë¡¢´Ø¿ô `foo` ¤ÎÆâÉô¤Î bind ¼°¤Ï¼¡¤Î¤è¤¦¤Ë½ñ¤¯¤³¤È¤¬²ÄÇ½¤Ç¤¢¤ë¡£

    bind(unlambda(f), _1)(x);

To get this functionality in BLL, the bind expression inside the `foo` function can be written as:

    bind(unlambda(f), _1)(x);

as explained in Section 5.9.1.1.

BLL ¤Ç¤Ï»°¤Ä¤Î¥×¥ì¡¼¥¹¥Û¥ë¥À¤·¤«ÄêµÁ¤µ¤ì¤Æ¤¤¤Ê¤¤¤Î¤ËÂÐ¤·¡¢ BB ¥é¥¤¥Ö¥é¥ê¤Ï¶å¤Ä¤Þ¤Ç¤Î¥×¥ì¡¼¥¹¥Û¥ë¥À¤ò¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤ë¡£ ¤³¤ì°Ê¾å¤Î¥×¥ì¡¼¥¹¥Û¥ë¥À¤òÄó¶¡¤·¤Ê¤¤º¬µò¤Ï¡¢¤¤¤«¤Ê¤ë STL ¥¢¥ë¥´¥ê¥º¥à¤Ç¼õÍý¤Ç¤¤ë´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤ÎºÇÂ¿¤Î°ú¿ô¤Î¿ô¤ÏÆó¤Ä¤Ç¤¢¤ë¤³¤È¤Ç¤¢¤ë¡£ BB ¥é¥¤¥Ö¥é¥ê¤Ë¤ª¤¤¤Æ¥×¥ì¡¼¥¹¥Û¥ë¥À¤Î¿ô¤Ï´ÊÃ±¤ËÁý²Ã¤µ¤»¤ë¤³¤È¤¬¤Ç¤¤ë¡£ BLL ¤Ç¤â²ÄÇ½¤Ç¤¢¤ë¤¬¡¢¤·¤«¤·¤è¤êÂ¿¤¯¤ÎÏ«ÎÏ¤òÉ¬Í×¤È¤¹¤ë¡£ ¸½ºß BLL ¤Ï¼Â°ú¿ô¤òÆâÉôÅª¤Ë¦Ë¥Õ¥¡¥ó¥¯¥¿¤Ë¤½¤Î¤Þ¤ÞÅÏ¤·¤Æ¤ª¤ê¡¢ ¥¿¥×¥ë¤Ë¥é¥Ã¥×¤·¤¿¤ê¤Ï¤·¤Ê¤¤¡£ ¤³¤ì¤Ï¡¢¹¤¯»ÈÍÑ¤µ¤ì¤Æ¤¤¤ë¤¤¤¯¤Ä¤«¤Î¥³¥ó¥Ñ¥¤¥é¤¬ºÇÅ¬²½¤Ë¤ª¤¤¤ÆÃæ´Ö¤Î¥¿¥×¥ë¤ò¼è¤êµî¤ë¤³¤È¤¬¤Ç¤¤Ê¤¤¤¿¤á¤Ç¤¢¤ë¡£ Ãæ´Ö¤Î¥¿¥×¥ë¤ÎÀ¸À®¤Ï¥Ñ¥Õ¥©¡¼¥Þ¥ó¥¹¤Ë½ÅÂç¤Ê±Æ¶Á¤òÍ¿¤¨¤Æ¤·¤Þ¤¦¡£ ÆÃ¤Ë¡¢Ã±½ã¤Ê(¤½¤·¤ÆºÇ¤â°ìÈÌ¤Ê)¦Ë¥Õ¥¡¥ó¥¯¥¿¤Î¾ì¹ç¤Ë¤Ï½ÅÍ×¤Ç¤¢¤ë¡£ ¤½¤·¤Æ¡¢¤è¤êÂ¿¤¯¤Î¥×¥ì¡¼¥¹¥Û¥ë¥À¤â²ÄÇ½¤Ç¤¢¤ë¤¬¡¢Ã±½ã¤Ê¦Ë¥Õ¥¡¥ó¥¯¥¿¤Î¥Ñ¥Õ¥©¡¼¥Þ¥ó¥¹¤Ë´Ø¤·¤ÆÂÅ¶¨¤·¤Ê¤¤ÀÞÃï°Æ¤òÁªÂò¤·¤¿¡£

The BB library supports up to nine placeholders, while the BLL defines only three placeholders. The rationale for not providing more, is that the highest arity of the function objects accepted by any STL algorithm is two. The placeholder count is easy to increase in the BB library. In BLL it is possible, but more laborous. The BLL currently passes the actual arguments to the lambda functors internally just as they are and does not wrap them inside a tuple object. The reason for this is that some widely used compilers are not capable of optimizing the intermediate tuple objects away. The creation of the intermediate tuples would cause a significant performance hit, particularly for the simplest (and thus the most common) lambda functors. We are working on a hybrid approach, which will allow more placeholders but not compromise the performance of simple lambda functors.

PrevŽ | Up | Ž Next
7. Practical considerationsŽ | Home | Ž 9. Contributors