compose, bind and a doubled ref problem
Hi! The code below compiles fine if You return to Value semantics and replace "const Vector&" with "Vector". But with reference to vector two compilers (Intel-7.0 and gcc-3.2.1) "throw" with a "reference to reference" error. How can I achieve what I want here? Do I have to apply cref somewhere? I do not want to change the type of "l" in this example. Also the signature of Vector::operator() and GetValue is fixed. Anything else may undergo a change. #include <iostream> #include <cstddef> #include "boost/function.hpp" #include "boost/bind.hpp" #include "boost/compose.hpp" struct Vector { double operator()(size_t j) const { std::cerr << "get value at index " << j << std::endl; return 99.0; } }; double GetValue(const Vector& V, size_t j) { return V(j); } double Add(double d1, double d2) { return d1 + d2; } int main() { using namespace boost; Vector V; function<double (const Vector&, size_t)> g = GetValue; function<double (const Vector&)> h1 = bind(g, _1, 5); function<double (const Vector&)> h2 = bind(g, _1, 4); function<double (double, double)> Adder = Add; function<double (const Vector&)> l = compose_f_gx_hx(Adder, h1, h2); std::cerr << l(V) << std::endl; }
From: "Markus Werle" <yg-boost-users@gmane.org>
The code below compiles fine if You return to Value semantics and replace "const Vector&" with "Vector". But with reference to vector two compilers (Intel-7.0 and gcc-3.2.1) "throw" with a "reference to reference" error.
How can I achieve what I want here?
[...]
#include "boost/bind.hpp" #include "boost/compose.hpp"
It's simple, don't use compose. :-) [...]
function<double (const Vector&)> l = compose_f_gx_hx(Adder, h1, h2);
Change to bind(Adder, bind(h1, _1), bind(h2, _1));
participants (2)
-
Markus Werle
-
Peter Dimov