This is the mail archive of the libstdc++@sourceware.cygnus.com mailing list for the libstdc++ project.


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

Re: bug in stl_function.h


Benjamin Kosnik wrote:
> 
> This in too, thanks. If you get a chance, please post a regression
> test for this.
> 
> 2000-04-18  Levente Farkas  <lfarkas@mindmaker.hu>
> 
>         * stl/bits/stl_function.h: Add bits so that const and non-const
>         both work.

thanks. but it's not in the standard and sgi include it in the 
stl/bits/stl_function.h, which is in the section 
20: General utilities library 
but there is no directory in testsuit for 20. anyway a simple test
which not compile without my modification (the line commented with:
// this line can't compile without it!!!).
------------
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;

template<typename _Selector, typename _Func>
  class Functor
  {
  public:
    Functor(_Selector _selector, _Func _func)
      : selector(_selector), func(_func) {}
    template<typename In>
      In& operator()(In& in)
      {
        func(selector(in));
        return in;
      }
  protected:
    _Selector selector;
    _Func func;
  };

template<typename _Selector, typename _Func>
  Functor<_Selector, _Func> functor(_Selector _selector, _Func _func)
  {
    return Functor<_Selector, _Func>(_selector, _func);
  }

class Inc : public unary_function<double, void>
{
public:
  void operator()(argument_type& d) { d += 1.0; }
};

int main()
{
  typedef vector<pair<int, double> > PairVector;
  PairVector vec;
  vec.push_back(make_pair(1, 0.3));
  vec.push_back(make_pair(33, 0.1));
  vec.push_back(make_pair(47, 0.8));

  transform(vec.begin(), vec.end(), ostream_iterator<double>(cout, " "),
            select2nd<PairVector::value_type>());
  cout << endl;
  // The output is  0.3 0.1 0.8

  // this line can't compile without it!!!
  transform(vec.begin(), vec.end(), vec.begin(),
            functor(select2nd<PairVector::value_type>(), Inc()));
  transform(vec.begin(), vec.end(), ostream_iterator<double>(cout, " "),
            select2nd<PairVector::value_type>());
  // The output is  1.3 1.1 1.8
  return 0;
}
------------
yours.

 -- lfarkas
 "The only thing worse than not knowing the truth is
  ruining the bliss of ignorance."

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