This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Bug report


Hello,

I thought I found a bug in Bjarne Stroustrup's book, but he says it's a
bug in your compiler. Here's the e-mail conversation I had with Bjarne:

--------------------------

I think that the definition of rotate_all on p. 521 is not
right. mem_fun should not be able to take &Shape::rotate as an
argument since this is presumably of type void (Shape::*)(int),
whereas mem_fun is looking for an argument of type R (T::*)(). I
tested this out with gcc version egcs-2.91.57 19980901 (egcs-1.1
release) and I indeed got the following error:

  test.cc: In function `void rotate_all(class list<Shape
*,__default_alloc_template<false,0> > &, int)':
  test.cc:18: no matching function for call to `mem_fun (void
(Shape::*)(int))'


Here's the code I used:


#include<iostream>
#include<list>
#include<functional>
#include<algorithm>

class Shape {
private:
  int rotatedness;
public:
  Shape(int i): rotatedness(i) {}
  void rotate(int angle)
  {
    rotatedness += angle;
    cout << "rotated by " <<  rotatedness
  << " degrees" << endl;
  }
};

void rotate_all(list<Shape*>& ls, int angle)
{
  for_each(ls.begin(), ls.end(),bind2nd(mem_fun(&Shape::rotate),angle));

}


int main() {
   list<Shape*> lst;
   lst.push_back(new Shape(3));
   rotate_all(lst,2);
   return 0;
}

===> It seems that the EGCS library is a bit behind the standard and
provides
        mem_fun1() rather than mem_fun().

        - Bjarne

Bjarne Stroustrup, AT&T Labs, http://www.research.att.com/~bs

---------------------------------------------------------------------
Dale Gerdemann                          dg@sfs.nphil.uni-tuebingen.de
Dept. of Linguistics                    +49 7071-29-74967 office
Section for Computational Linguistics   +49 7472-442298 home
University of Tuebingen                 Fax: +49 7071-550520
Kl. Wilhelmstr. 113
D-72074 Tuebingen, Germany
---------------------------------------------------------------------





Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]