Templates or the standard library

Bruce Eckel Eckel@CrestedButte.net
Sun Sep 6 12:59:00 GMT 1998


There's a problem here with either the template implementation or the
standard library. This compiles with KCC, BC++ 5.3 and even VC++ 6, but not
egcs 2.91.57 (it's a little confusing to know if this is 1.1 because of the
differing version numbers -- is there some way to tell? My 1.1 installation
was successful but it's hard to know if I'm actually using that or an old
one).

//: C20:Apply.cpp
// Using basic iterators
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;

template<class Cont, class PtrMemFun>
void apply(Cont c, PtrMemFun f) {
  Cont::iterator it = c.begin();
  while(it != c.end()) {
    (it->*f)();
    it++;
  }
}

class Z {
  int i;
public:
  Z(int ii) : i(ii) {}
  void g() { cout << "Z::g()" << endl; i++; }
  friend ostream&
  operator<<(ostream& os, const Z& z) {
    return os << z.i;
  }
};

int main() {
  ostream_iterator<Z> out(cout, " ");
  vector<Z> vz;
  for(int i = 0; i < 10; i++)
    vz.push_back(Z(i));
  copy(vz.begin(), vz.end(), out);
  cout << endl;
  apply(vz, &Z::g);
  copy(vz.begin(), vz.end(), out);
  cout << endl;
} ///:~
================================
Bruce Eckel     http://www.BruceEckel.com
Contains free electronic books: "Thinking in Java" and "Thinking in C++ 2e"






More information about the Gcc-bugs mailing list