 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ravi Guest
|
Posted: Tue Nov 23, 2004 10:56 am Post subject: Function argument type deduction |
|
|
One of the most important lessons I learnt from "Modern C++ design" is
that there is always a way (sometimes requiring considerable
ingenuity) to make the compiler deduce types at compile time if I can
deduce it at compile time. However, the following simple problem has
vexed me for several days.
I am trying to create a functor that applies a function to each
element of a vector in the following manner:
std::vector<float> myvec(10);
// Fill up vector here.
....
// Want the following to replace each element with its sine.
onEachElement( myvec, std::sin );
My first naive attempt at defining such a functor (which works for the
most part):
template <typename T> void onEachElement( std::vector<T> &vec,
T(&func)(T) )
{
for ( int indx = 0; indx < vec.size(); ++indx )
vec[indx] = func(vec[indx]);
}
However, the template above fails when "func" takes a template
parameter such as in the case below when called with a vector of
floats:
double mu( double x ) { return x+2.; }
T mu( T x ) { return x; }
Is there a way of doing this that would work with templates as well?
The compiler must somehow be forced to instantiate the template for
the type deduced from the first argument, and then take the address to
pass as the reference. Any help is greatly appreciated. If this is a
FAQ, I must have been searching for the wrong terms.
Regards,
Ravi
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gianluca Silvestri Guest
|
Posted: Tue Nov 23, 2004 6:43 pm Post subject: Re: Function argument type deduction |
|
|
"Ravi" <ravi (AT) gmx (DOT) de> ha scritto nel messaggio
news:c01b487b.0411221612.8c6b901 (AT) posting (DOT) google.com...
| Quote: | Is there a way of doing this that would work with templates as well?
The compiler must somehow be forced to instantiate the template for
the type deduced from the first argument, and then take the address to
pass as the reference. Any help is greatly appreciated. If this is a
FAQ, I must have been searching for the wrong terms.
|
Apart from using std::for_each, your own solution is to use 2 template
parameters:
template <typename T, typename U> void onEachElement( std::vector<T> &vec,
U(&func)(U) )
{
for ( int indx = 0; indx < vec.size(); ++indx )
vec[indx] = func(vec[indx]);
}
HTH,
Gianluca
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
jjr2004a Guest
|
Posted: Wed Nov 24, 2004 9:24 am Post subject: Re: Function argument type deduction |
|
|
The real question is why are you trying to reinvent the wheel? Chapter 18
of Stroustrup's book "Algorithms and Function Objects" explains the host of
algorithms available in the standard library to do almost anything to a
sequence of objects. It also contains tools to help adapt items like functions
for use with these algorithms.
I used the transform algorithm and the ptr_fun adapter to do your task.
You could use for_each but this solution seemed more straight forward.
Here's the complete example:
#include <iostream>
#include <vector>
#include <algorithm> // for transform
#include <functional> // for ptr_fun
#include <math.h> // for sin
using namespace std;
//
// sample transform program
//
int main(int argc, char** argv)
{
vector<double> myvec(10);
for (int i=0; i < 10; i++)
myvec[i] = i * 10.0 * 3.14159265 / 180.0;
transform(myvec.begin(), myvec.end(), myvec.begin(), ptr_fun(sin));
for (int i=0; i < 10; i++)
cout << "sine of " << i * 10 << " degrees = " << myvec[i] << endl;
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|