Topic: Of coarse, you can specify the template parameters....


Author: "Chris Tracy" <crtracy@students.uiuc.edu>
Date: 1998/10/24
Raw View
True, the STL algorithms default to vaule passing of the function objects.
To override the defaults, specify the template argument type to be a
reference:

(I'm assuming familiarity with the code in the previous post.  It is one of
the examples from The C++ Programming Language, Third Edition by Dr. Bjarne
Stoustrup)

    typedef std::list<double>::const_iterator i_t;
    typedef Sum<double> sd_t;
    std::for_each< i_t, sd_t& >(ld.begin(), ld.end(), s);
    // s.result() == actual sum

This ugly piece of code will avoid the pass-by-value overhead mentioned in
this thread.  I had to use the typedefs to get it to compile under VC5.
Without them it tells me that std::vector<double> isn't a namespace name (if
anyone knows the reason, feel free to email it to me).

I'm a novice of the language (in comparison with the people that frequent
this newsgroup), so if this solution was obvious, please forgive my noise.

Later,
Chris Tracy
Understanding starts with discussion. mailto:crtracy@uiuc.edu
Criticism welcome.     http://www.students.uiuc.edu/~crtracy/




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