6. Extending return type deduction system
PrevŽ | Ž | Ž Next

## 6. Extending return type deduction system

¤³¤Î¥»¥¯¥·¥ç¥ó¤Ç¤Ï¡¢¥æ¡¼¥¶ÄêµÁ¤Î±é»»»Ò¤ò°·¤¦¤¿¤á¤Ë¡¢ ÊÖ¤êÃÍ¤Î·¿¿äÏÀ¥·¥¹¥Æ¥à¤Î³ÈÄ¥¤¹¤ëÊýË¡¤ò²òÀâ¤¹¤ë¡£ BLL ¤Ï¥Ç¥Õ¥©¥ë¥È¤Î±é»»»Ò¤ÎÊÖ¤êÃÍ¤Î·¿¤òÄêµÁ¤·¤Æ¤¤¤ë¤¿¤á¡¢ Â¿¤¯¤Î¾ì¹ç¤Ë¤Ï¤³¤ì¤ÏÉ¬Í×¤Ê¤¤¡£ Îã¤¨¤Ð¡¢¤¹¤Ù¤Æ¤ÎÈæ³Ó±é»»»Ò¤Î¥Ç¥Õ¥©¥ë¥È¤ÎÊÖ¤êÃÍ¤Ï `bool` ¤Ç¤¢¤ê¡¢ ¥æ¡¼¥¶¤¬Èæ³Ó±é»»»Ò¤ÎÊÖ¤êÃÍ¤Î·¿¤ò bool ¤È¤·¤ÆÄêµÁ¤·¤Æ¤¤¤ë¸Â¤ê ÊÖ¤êÃÍ¤Î·¿¿äÏÀ¥¯¥é¥¹¤Î¿·¤·¤¤ÆÃ¼ì²½¤ò½ñ¤¯É¬Í×¤Ï¤Ê¤¤¡£ ¤·¤«¤·¡¢¤³¤ì¤¬ÉÔ²ÄÈò¤Ç¤¢¤ë¾ì¹ç¤â¤¢¤ë¡£

In this section, we explain how to extend the return type deduction system to cover user defined operators. In many cases this is not necessary, as the BLL defines default return types for operators. For example, the default return type for all comparison operators is `bool`, and as long as the user defined comparison operators have a bool return type, there is no need to write new specializations for the return type deduction classes. Sometimes this cannot be avoided, though.

¥ª¡¼¥Ð¡¼¥í¡¼¥É²ÄÇ½¤Ê¥æ¡¼¥¶ÄêµÁ¤Î±é»»»Ò¤Ï¡¢Ã±¹à¤Þ¤¿¤ÏÆó¹à±é»»»Ò¤Ç¤¢¤ë¡£ ¤½¤Î°ú¿ô¤Î¿ô¤´¤È¤Ë¡¢°Û¤ë±é»»»Ò¤ÎÊÖ¤êÃÍ¤Î·¿¤òÄêµÁ¤¹¤ë Æó¤Ä¤ÎÆÃÀ¥Æ¥ó¥×¥ì¡¼¥È¤¬¤¢¤ë¡£ ¤è¤Ã¤Æ¡¢¤³¤Î¥Æ¥ó¥×¥ì¡¼¥È¤ò»È¤¤ÆÃ¼ì²½¤¹¤ë¤³¤È¤Ë¤è¤ê¡¢ÊÖ¤êÃÍ¤Î·¿¿äÏÀ¥·¥¹¥Æ¥à¤ò³ÈÄ¥¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£ Ã±¹à´Ø¿ô¤Î¤¿¤á¤Î¥Æ¥ó¥×¥ì¡¼¥È¤Ï¡¢ `plain_return_type_1<Action, A>` ¤È `return_type_1<Action, A>` ¤Ç¤¢¤ê¡¢Æó¹à´Ø¿ô¤Î¤¿¤á¤Î¤â¤Î¤Ï¡¢ `plain_return_type_2<Action, A, B>` ¤È `return_type_2<Action, A, B>` ¤Ç¤¢¤ë¡£

The overloadable user defined operators are either unary or binary. For each arity, there are two traits templates that define the return types of the different operators. Hence, the return type system can be extended by providing more specializations for these templates. The templates for unary functors are `plain_return_type_1<Action, A>` and `return_type_1<Action, A>` , and `plain_return_type_2<Action, A, B>` and `return_type_2<Action, A, B>` respectively for binary functors.

¤³¤ì¤éÁ´¤Æ¤Î¥Æ¥ó¥×¥ì¡¼¥È¤ËÂÐ¤¹¤ëÂè°ì°ú¿ô(`Action`) ¤Ï¡¢ ±é»»»Ò¤ò»ØÄê¤·¤Æ¤¤¤ë ¥¢¥¯¥·¥ç¥ó¥¯¥é¥¹¤Ç¤¢¤ë¡£ Æ±¤¸ÊÖ¤êÃÍ¤Î·¿¤Îµ¬Â§¤ò»ý¤Ã¤¿±é»»»Ò¤Ï¡¢ ¥¢¥¯¥·¥ç¥ó¥°¥ë¡¼¥× ¤Ë¤Þ¤È¤á¤é¤ì¡¢ ¥¢¥¯¥·¥ç¥ó¥¯¥é¥¹¤È¥¢¥¯¥·¥ç¥ó¥°¥ë¡¼¥×¤Î¤ß¤¬±é»»»Ò¤ÎÛ£Ëæ¤µ¤ò²ò·è¤¹¤ë¡£ Îã¤òµó¤²¤ë¤È¡¢`operator+`¤ò¼¨¤¹¥¢¥¯¥·¥ç¥ó·¿ `arithmetic_action<plus_action>` ¤¬¤¢¤ë¡£ Â¾¤Î¥¢¥¯¥·¥ç¥ó·¿¤Î´°Á´¤Ê¥ê¥¹¥È¤ò Table 2 ¤Ë¼¨¤¹¡£

The first parameter (`Action`) to all these templates is the action class, which specifies the operator. Operators with similar return type rules are grouped together into action groups, and only the action class and action group together define the operator unambiguously. As an example, the action type `arithmetic_action<plus_action>` stands for `operator+`. The complete listing of different action types is shown in Table 2.

Ã±¹à´Ø¿ô¤Î¾ì¹ç¤Î `A` ¤äÆó¹à´Ø¿ô¤Î¾ì¹ç¤Î `A` ¤ä `B` ¤Î»Ä¤ê¤Î¥Æ¥ó¥×¥ì¡¼¥È°ú¿ô¤Ï¡¢±é»»»Ò¸Æ½Ð¤·¤Î°ú¿ô¤Î·¿¤ò¼¨¤·¤Æ¤¤¤ë¡£ Æó¤Ä¤Î¥Æ¥ó¥×¥ì¡¼¥È·² `plain_return_type_`n`` ¤È `return_type_`n`` ¤Ï¡¢(`n` ¤Ï£±¤Þ¤¿¤Ï£²) °ú¿ô¤Î·¿¤ÎÄó¼¨¤Î»ÅÊý¤Ë¤ª¤¤¤Æ°Û¤Ê¤ë¡£ Á°¼Ô¤Î¥Æ¥ó¥×¥ì¡¼¥È¤Ç¤Ï¡¢°ú¿ô¤Î·¿¤Ï¾ï¤Ë»²¾È·¿¤Ç¤Ê¤¯¡¢ ¤Þ¤¿¡¢const ¤ä volatile ¤È¤¤¤Ã¤¿½¤¾þ»Ò¤â¤Ä¤¤¤Æ¤¤¤Ê¤¤¡£ ¤³¤ì¤Ë¤è¤ê¡¢°ìÈÌÅª¤Ë³Æ¥æ¡¼¥¶ÄêµÁ¤Î±é»»»Ò¤ä±é»»»Ò·²¤´¤È¤Ë°ì¤Ä¤ÎÆÃ¼ì²½¤ò¹Ô¤¦¤À¤±¤Ç½½Ê¬¤È¤Ê¤ë¤¿¤á¡¢ÆÃ¼ì²½¤¬´ÊÃ±¤Ë¤Ê¤ë¡£ °ìÊý¡¢ÆÃÄê¤Î±é»»»Ò¤òÆ±¤¸·¿¤Î°ú¿ô¤Ë°Û¤Ê¤Ã¤¿ const/volatile ¤Î½¤¾þ»Ò¤ò¤Ä¤±¤¿¤ê¡¢¥ª¡¼¥Ð¡¼¥í¡¼¥É¤·¤¿±é»»»Ò¤ÎÊÖ¤êÃÍ¤Î·¿¤¬°Û¤Ê¤ë¾ì¹ç¤Ë¤Ï¡¢¤è¤êºÙ¤«¤¤Áàºî¤¬É¬Í×¤È¤Ê¤ë¡£ ¤è¤Ã¤Æ¡¢¸å¼Ô¤Î¥Æ¥ó¥×¥ì¡¼¥È¤Ç¤Ï¡¢°ú¿ô¤Î·¿¤Ï const/volatile ¤Î½¤¾þ»Ò¤âÊÑ¹¹¤»¤º¡¢Èó»²¾È·¿¤â¤½¤Î¤Þ¤Þ¤Ë¤¹¤ë¡£ ·çÅÀ¤Ï¾åµ¤Î¤è¤¦¤Ê¥ª¡¼¥Ð¡¼¥í¡¼¥É¤·¤¿±é»»»Ò¤Î¤¿¤á¤Ë¡¢ºÇ°¤Î¾ì¹ç½½Ï»¸Ä¤Î `return_type_2` ¤ÎÆÃ¼ì²½¤¬É¬Í×¤È¤Ê¤ë¤«¤â¤·¤ì¤Ê¤¤¤³¤È¤Ç¤¢¤ë¡£

The latter parameters, `A` in the unary case, or `A` and `B` in the binary case, stand for the argument types of the operator call. The two sets of templates, `plain_return_type_`n`` and `return_type_`n`` (`n` is 1 or 2) differ in the way how parameter types are presented to them. For the former templates, the parameter types are always provided as non-reference types, and do not have const or volatile qualifiers. This makes specializing easy, as commonly one specialization for each user defined operator, or operator group, is enough. On the other hand, if a particular operator is overloaded for different cv-qualifications of the same argument types, and the return types of these overloaded versions differ, a more fine-grained control is needed. Hence, for the latter templates, the parameter types preserve the cv-qualifiers, and are non-reference types as well. The downside is, that for an overloaded set of operators of the kind described above, one may end up needing up to 16 `return_type_2` specializations.

¥æ¡¼¥¶ÄêµÁ¤Î·¿ `X`¡¢`Y`¡¢`Z` ¤ËÂÐ¤·¤Æ±é»»»Ò¤ò¥ª¡¼¥Ð¡¼¥í¡¼¥É¤·¤¿¤È¤¹¤ë¡£

    Z operator+(const X&, const Y&);
    Z operator-(const X&, const Y&);

Suppose the user has overloaded the following operators for some user defined types `X`, `Y` and `Z`:

    Z operator+(const X&, const Y&);
    Z operator-(const X&, const Y&);

¤½¤·¤Æ°Ê²¼¤Ç¤Ï¡¢º¸Â¦¤Î°ú¿ô¤Î·¿¤¬ `X`¤Ç¤¢¤ê¡¢±¦Â¦¤Î°ú¿ô¤Î·¿¤¬ `Y` ¤Ç¤¢¤ì¤Ð¡¢¤½¤Î¤è¤¦¤ÊÆó¹à±é»»¤Î·ë²Ì¤Î·¿¤Ï `Z` ¤Ç¤¢¤ë¤È¤¤¤¦ÆÃ¼ì²½¤Îµ½Ò¤òÄÉ²Ã¤·¤Æ¤¤¤ë¡£

    namespace boost {
    namespace lambda {

    template<class Act>
    struct plain_return_type_2<arithmetic_action<Act>, X, Y> {
      typedef Z type;
    };

    }
    }

Now, one can add a specialization stating, that if the left hand argument is of type `X`, and the right hand one of type `Y`, the return type of all such binary arithmetic operators is `Z`:

    namespace boost {
    namespace lambda {

    template<class Act>
    struct plain_return_type_2<arithmetic_action<Act>, X, Y> {
      typedef Z type;
    };

    }
    }

¤³¤ÎÆÃ¼ì²½¤òÄêµÁ¤¹¤ë¤³¤È¤Ë¤è¤ê¡¢ BLL ¤ÏÀµ¤·¤¯¾åµ¤ÎÆó¤Ä¤Î±é»»»Ò¤ÎÊÖ¤êÃÍ¤Î·¿¤ò¿äÏÀ¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£ ÆÃ¼ì²½¤Ï¼çÍ×¤Ê¥Æ¥ó¥×¥ì¡¼¥È¤È¤È¤â¤Ë¡¢Ì¾Á°¶õ´Ö `::boost::lambda`¤Ë Â¸ºß¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£ ´ÊÃ±¤Î¤¿¤á¤Ë¡¢°Ê²¼¤ÎÎã¤Ç¤Ï¤³¤ÎÌ¾Á°¶õ´Ö¤ÎÄêµÁ¤Ï¾ÊÎ¬¤¹¤ë¡£
Having this specialization defined, BLL is capable of correctly deducing the return type of the above two operators. Note, that the specializations must be in the same namespace, `::boost::lambda`, with the primary template. For brevity, we do not show the namespace definitions in the examples below.

±é»»»Ò¥°¥ë¡¼¥×¤ÎÆÃ¼ì²½¤Ë²Ã¤¨¤Æ¡¢ ¸Ä¡¹¤Î±é»»»Ò¤´¤È¤ËÆÃ¼ì²½¤ò¹Ô¤¦¤³¤È¤â²ÄÇ½¤Ç¤¢¤ë¡£ Îã¤¨¤Ð¡¢¿·¤·¤¯°ú¿ô¤Î·¿¤¬¡¢`X` ¤È `Y` ¤Ç¤¢¤ë»»½Ñ±é»»»Ò¤òÄÉ²Ã¤·¤¿¤È¤¹¤ë¡£

    X operator*(const X&, const Y&);

Á´¤Æ¤Î»»½Ñ±é»»»Ò¤ËÂÐ¤¹¤ëºÇ½é¤Îµ¬Â§¤Ç¤Ï¡¢ ÊÖ¤êÃÍ¤Î·¿¤Ï `Z` ¤È»ØÄê¤·¤Æ¤¤¤ë¡£ ¤³¤ì¤Ï¡¢¤³¤Î¾ì¹ç¤Ë¤ÏÌÀ¤é¤«´Ö°ã¤Ã¤Æ¤¤¤ë¡£ ¤è¤Ã¤Æ¡¢¾êÍ¾±é»»»Ò¤ËÂÐ¤·¤Æ¼¡¤Î¤è¤¦¤Ë¿·¤·¤¤µ¬Â§¤òÄó¶¡¤¹¤ë¡£

    template<>
    struct plain_return_type_2<arithmetic_action<multiply_action>, X, Y> {
      typedef X type;
    };

It is possible to specialize on the level of an individual operator as well, in addition to providing a specialization for a group of operators. Say, we add a new arithmetic operator for argument types `X` and `Y`:

    X operator*(const X&, const Y&);

Our first rule for all arithmetic operators specifies that the return type of this operator is `Z`, which obviously is not the case. Hence, we provide a new rule for the multiplication operator:

    template<>
    struct plain_return_type_2<arithmetic_action<multiply_action>, X, Y> {
      typedef X type;
    };

ÆÃ¼ì²½¤Ë¤è¤ê¡¢°ú¿ô¤Î·¿¤«¤éÊÖ¤êÃÍ¤Î·¿¤Ø¤ÎÇ¤°Õ¤Î¥Þ¥Ã¥Ô¥ó¥°¤òÄêµÁ¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£ ¼¡¤Î¤è¤¦¤Ë¡¢Í×ÁÇ¤Î·¿¤ò¥Æ¥ó¥×¥ì¡¼¥È¤È¤·¤¿¿ô³ØÅª¤Ê¥Ù¥¯¥È¥ë¤¬¤¢¤Ã¤¿¤È¤¹¤ë¡£

    template <class T> class my_vector;

The specializations can define arbitrary mappings from the argument types to the return type. Suppose we have some mathematical vector type, templated on the element type:

    template <class T> class my_vector;

Í×ÁÇ¤Î·¿¤Î´Ö¤Ë²ÃË¡±é»»»Ò¤¬ÄêµÁ¤µ¤ì¤Æ¤¤¤ë¸Â¤ê¡¢ ¤¤¤«¤Ê¤ëÆó¤Ä¤Î `my_vector` ¤Î¥¤¥ó¥¿¥ó¥¹¤Î´Ö¤Ë¤â²ÃË¡±é»»»Ò¤¬ÄêµÁ¤µ¤ì¤ë¤È¤¹¤ë¡£ ¤µ¤é¤Ë¡¢¤½¤Î·ë²Ì¤Î `my_vector` ¤ÎÍ×ÁÇ¤Î·¿¤Ï¡¢ Æó¤Ä¤Î `my_vector` ¤ÎÍ×ÁÇ¤Î²ÃË¡¤Î·ë²Ì¤Î·¿¤ÈÆ±¤¸¤Ç¤¢¤ë¤È¤¹¤ë¡£ Îã¤¨¤Ð¡¢`my_vector<int>` ¤È`my_vector<double>` ¤Î²ÃË¡¤Î·ë²Ì¤Ï `my_vector<double>` ¤È¤Ê¤ë¡£ BLL ¤ÏÀ°¿ô¡¢ÉâÆ°¾®¿ô¡¢Ê£ÁÇ¿ô¤Î´Ö¤Î°ÅÌÛÅª¤ÊÁÈ¹þ¤ß¤ÎÉ¸½àÅª¤Ê·¿ÊÑ´¹¤ò¼Â¸½¤¹¤ëÆÃÀ¥¯¥é¥¹¤ò»ý¤Ã¤Æ¤¤¤ë¡£ BLL ¤Îµ¡Ç½¤òÍøÍÑ¤¹¤ì¤Ð¡¢¾åµ¤Î²ÃË¡±é»»»Ò¤Ï¼¡¤Î¤è¤¦¤ËÄêµÁ¤Ç¤¤ë¡£

    template<class A, class B>
    my_vector<typename return_type_2<arithmetic_action<plus_action>, A, B>::type>
    operator+(const my_vector<A>& a, const my_vector<B>& b)
    {
      typedef typename
        return_type_2<arithmetic_action<plus_action>, A, B>::type res_type;
      return my_vector<res_type>();
    }

Suppose the addition operator is defined between any two `my_vector` instantiations, as long as the addition operator is defined between their element types. Furthermore, the element type of the resulting `my_vector` is the same as the result type of the addition between the element types. E.g., adding `my_vector<int>` and `my_vector<double>` results in `my_vector<double>`. The BLL has traits classes to perform the implicit built-in and standard type conversions between integral, floating point, and complex classes. Using BLL tools, the addition operator described above can be defined as:

    template<class A, class B>
    my_vector<typename return_type_2<arithmetic_action<plus_action>, A, B>::type>
    operator+(const my_vector<A>& a, const my_vector<B>& b)
    {
      typedef typename
        return_type_2<arithmetic_action<plus_action>, A, B>::type res_type;
      return my_vector<res_type>();
    }

`my_vector` ¤Î²Ã»»¤Î·¿¿äÏÀ¤òÀµ³Î¤Ë¹Ô¤¦¤¿¤á¤Ë¡¢¼¡¤Î¤è¤¦¤ËÄêµÁ¤Ç¤¤ë¡£

    template<class A, class B>
    class plain_return_type_2<arithmetic_action<plus_action>,
                               my_vector<A>, my_vector<B> > {
      typedef typename
        return_type_2<arithmetic_action<plus_action>, A, B>::type res_type;
    public:
      typedef my_vector<res_type> type;
    };

BLL ¤Î¥Æ¥ó¥×¥ì¡¼¥È `return_type_2` ¤Î¼Âºß¤¹¤ëÆÃ¼ì²½¤òºÆÍøÍÑ¤·¤Æ¤¤¤ë¡£ ¤½¤Î¤¿¤á¤Ë¤Ï¡¢°ú¿ô¤Î·¿¤¬»²¾È·¿¤Ç¤¢¤ë¤³¤È¤¬É¬Í×¤Ç¤¢¤ë¡£

To allow BLL to deduce the type of `my_vector` additions correctly, we can define:

    template<class A, class B>
    class plain_return_type_2<arithmetic_action<plus_action>,
                               my_vector<A>, my_vector<B> > {
      typedef typename
        return_type_2<arithmetic_action<plus_action>, A, B>::type res_type;
    public:
      typedef my_vector<res_type> type;
    };

Note, that we are reusing the existing specializations for the BLL `return_type_2` template, which require that the argument types are references.

Table 2. Action types

`+` | `arithmetic_action<plus_action>`
`-` | `arithmetic_action<minus_action>`
`*` | `arithmetic_action<multiply_action>`
`/` | `arithmetic_action<divide_action>`
`%` | `arithmetic_action<remainder_action>`
`+` | `unary_arithmetic_action<plus_action>`
`-` | `unary_arithmetic_action<minus_action>`
`&` | `bitwise_action<and_action>`
`|` | `bitwise_action<or_action>`
`~` | `bitwise_action<not_action>`
`^` | `bitwise_action<xor_action>`
`<<` | `bitwise_action<leftshift_action_no_stream>`
`>>` | `bitwise_action<rightshift_action_no_stream>`
`&&` | `logical_action<and_action>`
`||` | `logical_action<or_action>`
`!` | `logical_action<not_action>`
`<` | `relational_action<less_action>`
`>` | `relational_action<greater_action>`
`<=` | `relational_action<lessorequal_action>`
`>=` | `relational_action<greaterorequal_action>`
`==` | `relational_action<equal_action>`
`!=` | `relational_action<notequal_action>`
`+=` | `arithmetic_assignment_action<plus_action>`
`-=` | `arithmetic_assignment_action<minus_action>`
`*=` | `arithmetic_assignment_action<multiply_action>`
`/=` | `arithmetic_assignment_action<divide_action>`
`%=` | `arithmetic_assignment_action<remainder_action>`
`&=` | `bitwise_assignment_action<and_action>`
`=|` | `bitwise_assignment_action<or_action>`
`^=` | `bitwise_assignment_action<xor_action>`
`<<=` | `bitwise_assignment_action<leftshift_action>`
`>>=` | `bitwise_assignment_action<rightshift_action>`
`++` | `pre_increment_decrement_action<increment_action>`
`--` | `pre_increment_decrement_action<decrement_action>`
`++` | `post_increment_decrement_action<increment_action>`
`--` | `post_increment_decrement_action<decrement_action>`
`&` | `other_action<address_of_action>`
`*` | `other_action<contents_of_action>`
`,` | `other_action<comma_action>`

PrevŽ | Up | Ž Next
5. Lambda expressions in detailsŽ | Home | Ž 7. Practical considerations