Topic: binder2nd problems (not only) with Visual C++ 5


Author: Ralf Stoffels <stoffels@faho.rwth-aachen.de>
Date: 1998/04/16
Raw View
> 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.
Yes, this is a bug in VC++.
The same bug is in mem_fun_ref_t.

> 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!)


The design of <functional> is very poor (i.e. not very useable) because
you cannot handle functional objects with bind... which use reference
parameters.
(I posted this problem in several C++-newsgroups but never got a
 real feedback.)
Here is another simple example:
  struct A {}
  a1, a2;

  bool f(const A& a, const A& b)
  {return true;}

  bool b = std::bind2nd(std::ptr_fun(&f), a2) (a1);  // reference to reference
error



Problem description:
  ptr_fun returns a pointer_to_binary_function<const A&, const A&, bool>  // ok

  bind2nd returns a
  binder2nd<pointer_to_binary_function<const A&, const A&, bool> >
  with the ctor:
  binder2nd (const pointer_to_binary_function<const A&, const A&, bool>& x,
             const const A&&);    //  :-(
  and the
  bool operator () (const const A &&) const;  //  :-(



Kevin S. Van Horn has a solution for those problems:
http://www.xmission.com/~ksvhsoft/code/fct_objs.html



Ralf
---
[ 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              ]