M
Maitre Bart
What I want to perform is calling a member function of container 1
(CRunner), using as argument the value of a container 2 (CNames). The
purpose is to transfer values from C2 into C1 using a specific C1 member
function. Both containers contains the same number of elements (or at least,
C1 is larger than C2).
As suggested by S. Meyers (and others), I would like to use an STL algorithm
instead of a custom loop. Let say I have the following:
class CRunner{
string m_name;
int m_order;
public:
bool SetOrder(int a_order) {m_order = a_order; return true;}
bool SetName(const string& a_name) {m_name = a_name; return true;}
};
vector<string> NameTable;
vector<CRunner> RunnerTable;
I created a template functor/function pair to be used in for_each():
template<class R, class T>
class mem_fun_iter_t : unary_function<T*, R>
{
public:
explicit mem_fun_iter_t(R (T::*p)(const T::iterator::value_type&),
vector<T>::iterator i) : pmf(p), iter(i) {}
R operator()(T* p) const {return (p->*pmf)(*iter++);}
private:
R (T::*pmf)(const T::iterator::value_type&);
vector<T>::iterator iter;
};
template<class R, class T>
inline static mem_fun_iter_t<R, T> mem_fun_iter(R (T::*p)(const
T::iterator::value_type&), vector<T>::iterator i)
{
return mem_fun_iter_t<R, T, vector<T> >(p, i);
}
(...unfortunetaly, I wasn't able to specify a generic parameter for
'vector', without getting all other kinds of errors...)
So that once both containers are initalized and filled, I can safely call:
for_each(NameTable.begin(), NameTable.end(), mem_fun_iter(CRunner::SetName,
RunnerTable.begin()));
But my compiler always generate this error:
[...] error C2784: 'mem_fun_iter_t<R,T> mem_fun_iter(R (T::*)(const
generic-type-264 &),generic-type-265)': could not deduce template argument
for '<Unknown>' from 'bool (RUNNER::*)(const string &)'
Is it my compiler (MSVS6) or is it a real deduction problem (missing
parameter)?
Is it a problem of algorithm? Is there any way to do the same with another
algorithm (such as transform(), etc.)?
Is there any way to reuse mem_fun* (with a binder or not) instead of the
above custom template functor/function pair?
Is there a way to make the call "end-of-container"-proof by using
back_inserter()?
I ask the those questions because I tried all those combinations without
success.
The task look simple, but caused me frustrations over the past 3 days.
As a second part, what would be the implications if rather than having
'vector<string> NameTable' I had:
vector<CNames> NameTable;
with:
class CNames
{
//...
public:
string GetName();
//...
};
Thanks for helping.
(CRunner), using as argument the value of a container 2 (CNames). The
purpose is to transfer values from C2 into C1 using a specific C1 member
function. Both containers contains the same number of elements (or at least,
C1 is larger than C2).
As suggested by S. Meyers (and others), I would like to use an STL algorithm
instead of a custom loop. Let say I have the following:
class CRunner{
string m_name;
int m_order;
public:
bool SetOrder(int a_order) {m_order = a_order; return true;}
bool SetName(const string& a_name) {m_name = a_name; return true;}
};
vector<string> NameTable;
vector<CRunner> RunnerTable;
I created a template functor/function pair to be used in for_each():
template<class R, class T>
class mem_fun_iter_t : unary_function<T*, R>
{
public:
explicit mem_fun_iter_t(R (T::*p)(const T::iterator::value_type&),
vector<T>::iterator i) : pmf(p), iter(i) {}
R operator()(T* p) const {return (p->*pmf)(*iter++);}
private:
R (T::*pmf)(const T::iterator::value_type&);
vector<T>::iterator iter;
};
template<class R, class T>
inline static mem_fun_iter_t<R, T> mem_fun_iter(R (T::*p)(const
T::iterator::value_type&), vector<T>::iterator i)
{
return mem_fun_iter_t<R, T, vector<T> >(p, i);
}
(...unfortunetaly, I wasn't able to specify a generic parameter for
'vector', without getting all other kinds of errors...)
So that once both containers are initalized and filled, I can safely call:
for_each(NameTable.begin(), NameTable.end(), mem_fun_iter(CRunner::SetName,
RunnerTable.begin()));
But my compiler always generate this error:
[...] error C2784: 'mem_fun_iter_t<R,T> mem_fun_iter(R (T::*)(const
generic-type-264 &),generic-type-265)': could not deduce template argument
for '<Unknown>' from 'bool (RUNNER::*)(const string &)'
Is it my compiler (MSVS6) or is it a real deduction problem (missing
parameter)?
Is it a problem of algorithm? Is there any way to do the same with another
algorithm (such as transform(), etc.)?
Is there any way to reuse mem_fun* (with a binder or not) instead of the
above custom template functor/function pair?
Is there a way to make the call "end-of-container"-proof by using
back_inserter()?
I ask the those questions because I tried all those combinations without
success.
The task look simple, but caused me frustrations over the past 3 days.
As a second part, what would be the implications if rather than having
'vector<string> NameTable' I had:
vector<CNames> NameTable;
with:
class CNames
{
//...
public:
string GetName();
//...
};
Thanks for helping.