Boost logo

Boost Users :

From: Markus Werle (yg-boost-users_at_[hidden])
Date: 2002-12-04 07:40:47


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;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net