Topic: binder2nd problems with Visual C++ 5


Author: leonidasak@aol.com (LeonidasAK)
Date: 1998/04/14
Raw View
Peter Nunn writes:

>for_each(objects.begin(), objects.end(),
>    binder2nd<mem_fun1_ref_t<bool, c2, const c1 &> >(
>       mem_fun1_ref_t<bool, c2, const c1 &>(c2::refresh), obj));

I really did not look into the above too closely to try to understand it.  I
presume that you wish to arrange it so that each member of object  does
something like:
                                  objectMember.refresh(obj);
If that is not the case, or if you're already aware that you can use a function
object, you should disregard my "solution" below.  I should mention though that
your problem might not be binder2nd, but mem_fun1_ref_t.  You might wish to
take a look at the thread [for_each and member functions] in
comp.lang.c++.moderated.

I did take some liberties with your code, so as to make it fit my style.  I
also threw in some added baggage to shut up the compiler when it was
complaining about constness and insertion of c2's into a vector.  The main
point is, though, the use of the function object "c2FuncObj" that holds an obj,
and arranges to pass it as a parameter to refresh() through its operator().

// First program line
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;

class c1
{
public:
   void an_op() const                // added const
   {
      cout << "in an_op" << endl;
   }
};

class c2
{
public:
   bool refresh(const c1 &obj) const   // added const
   {
      cout << "in refresh" << endl;

      obj.an_op();
      return(true);                    // must return a value
   }

   int a;       // added this as easy fix
                // for operator< and operator==;
                // need these to insert c2 objects
                // into a vector
};

bool operator<(const c2& lhs, const c2& rhs)
{
   return(lhs.a < rhs.a);
}

bool operator==(const c2& lhs, const c2& rhs)
{
   return(lhs.a == rhs.a);
}

// here's a function object that stores a reference
// to your "c1 obj", and arranges to call "refresh"
// with obj as parameter
class c2FuncObj
{
public:
   explicit c2FuncObj(const c1& aC1) : itsC1(aC1)
   {
      cout << "in c2FuncObj constructor" << endl;
   }

   bool operator()(const c2 &aC2)
   {
      cout << "in operator()" << endl;

      return(aC2.refresh(itsC1));
   }

private:
   const c1& itsC1;
};

void update_objects(vector<c2>& v)
{
   c1 obj;

   for_each(v.begin(), v.end(), c2FuncObj(obj));
}

int main()
{
   vector<c2> objects(3);

   update_objects(objects);

   return(0);
}

// last program line

I Hope this helps.

Leonidas



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Peter Nunn" <peter.nunn@purist.com>
Date: 1998/04/07
Raw View
I have been working on a project that uses the vector and want to pass the
following to each element of the vector

#include <vector>
#include <algorithm>
#include <functional>

class c1 {
  public:
    void an_op() {}
};

class c2 {
  public:
     bool refresh(const c1 &obj) { obj.an_op(); }
};

vector<c2> objects;

void update_objects()
{
  c1 obj;

  for_each(objects.begin(), objects.end(),
    binder2nd<mem_fun1_ref_t<bool, c2, const c1 &> >(
       mem_fun1_ref_t<bool, c2, const c1 &>(c2::refresh), obj));
}

The above code is abstracted a little as refresh does nothing.  With the C++
compiler it says that it cannot convert const c2 to const c2 *&.
The only way I can get it to compile is to change functional to support
non-const operator (), remove some const declarations that are unnecessary,
and changing the template for mem_fun1_ref_t from
struct mem_fun1_ref_t : public binary_function<T *, A, R> to
struct mem_fun1_ref_t : public binary_function<T, A, R>
It seems that the mem_fun1_ref_t is a little odd as the first_argument type
is T not T* as the binary function template states.
The removal of const around some arguments I can live with but the
mem_fun1_ref_t seems to be not designed to handle insertion into a
unary_function derived class (and have it work!)
Any help or comments would be welcomed.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]