C ++ boost :: lambda :: ret equivalent in phoenix

Boost lambda allows you to overwrite the output return type using a template ret<T>. I tried to find the equivalent in phoenix but could not find it.

Is there an equivalent in phoenix? I know how to make my own replacement, but I would prefer. thank

+3
source share
2 answers

Rewrite: I missed my first answer (it was late), let me try again.

Let me give some exposure for people like me who might miss your moment for the first time. In boost :: lambda, when using custom types in operator expressions, you must use the ret <> function to override the return type output. This is due to the fact that the lambda type subtraction system directly supports native types (and stl? I don’t remember). A brief example:

using namespace boost::lambda;

struct add_t{
    add_t(int i) : i(i) {};
    add_t operator+(const add_t& other) const{
        return add_t(i + other.i);
    }
    int i;
};

(_1 + _2)(add_t(38), add_t(4));           // RETURN TYPE DEDUCTION FAILS
ret<add_t>(_1 + _2)(add_t(38), add_t(4)); // OK

In phoenix, however, no hints are needed (note that literals and non-constant temporary files cannot appear in the phoenix argument list):

using namespace boost::phoenix;

add_t i(38), j(4);
(_1 + _2)(i, j);    // JUST FINE

; , . , , , stl container/container . phoenix type_deduction.hpp.

, : phoenix?

struct add_ret_t{
    add_ret_t(int i) : i(i) {};
    int i;
};

struct add_t{
    add_t(int i) : i(i) {};
    add_ret_t operator+(const add_t& other) const{
        return add_ret_t(i + other.i);
    }
    int i;
};

, ret:

using namespace boost::lambda;

ret<add_ret_t>(_1 + _2)(add_t(38), add_t(4)); // OK

phoenix ( ?), , phoenix. , , type_deduction.hpp .

, . result_of_operation boost/spirit/home/phoenix/operator/arithmetic.hpp( 39-56 , boost 1.43) . , , , - , typedef, . (codepad src):

using namespace boost::phoenix;

namespace boost{ namespace phoenix{

//override add_t addition to give add_ret_t
template <> struct result_of_plus<add_t&, add_t&> { typedef add_ret_t type; };

//override int addition to give char
template <> struct result_of_plus<int&, int&> { typedef char type; };

}}

int main()
{
    add_t i = 1, j = 7;
    std::cout << ((_1 + _2)(i, j)).i << std::endl;

    int k = 51, l = 37;
    std::cout << ((_1 + _2)(k, l)) << std::endl;

    return 0;
}

, , ret, . , .

+8

AFAIK, ( - ) . , , .

0

Source: https://habr.com/ru/post/1746235/


All Articles