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));
ret<add_t>(_1 + _2)(add_t(38), add_t(4));
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);
; , . , , , 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));
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{
template <> struct result_of_plus<add_t&, add_t&> { typedef add_ret_t type; };
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, . , .