Re: Algorithm template specialization problem
In article <1153910949.367265.235540@m79g2000cwm.googlegroups.com>,
Sherrie Laraurens <sherrielaraurens@hotmail.com> wrote:
I've considered using a function object, but the algorithm I'm trying
to perform is not a for_each style thing, it needs to go through all
the elements in the iterator range, do some things, then modify the
elements accordingly.
The 1998 standard is silent on the following but the 2006 draft
indicates by an added note under consideration explicitly allows
something like the following:
#include <vector>
#include <algorithm>
#include <iterator>
template <class T>
struct A
{
T val;
};
template <class T>
struct B
{
T val1;
T val2;
};
struct foo
{
template <class T>
void operator () (A<T> &x)
{
++x.val;
}
template <class T>
void operator () (B<T> &x)
{
++x.val1;
++x.val2;
}
};
int main()
{
std::vector<A<double> > data_a;
std::vector<B<double> > data_b;
std::for_each(data_a.begin(),data_a.end(),foo());
std::for_each(data_b.begin(),data_b.end(),foo());
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]