Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Deduce non-type template parameter return value

Reply
Thread Tools

Deduce non-type template parameter return value

 
 
Chris McAce
Guest
Posts: n/a
 
      02-08-2012
Hi,

I've written a function template for applying a free function to every
element of a std::vector:

#include <vector>

template<class T, T F(T)>
std::vector<T> map(const std::vector<T>& from)
{
std::vector<T> to;
to.reserve(from.size());
for (typename std::vector<T>::const_iterator iter = from.begin();
iter != from.end(); ++iter) {
to.push_back(F(*iter));
}
return to;
}

This can be used as follows:

#include <cmath>

int main()
{
std::vector<float> angles;
angles.push_back(1);
angles.push_back(2);
angles.push_back(3);
std::vector<float> sines = map<float, std::sin>(angles);
}

As you can see, std::sin is passed as a non-type template parameter.
However, I'd like to get rid of the the first parameter (float in the
example) so the call would become map<std::sin>(angles). Is there a
way to do this in C++03 or C++11?

Please note that I don't want to pass function pointers at run-time;
the mapping function (here, std::sin) is known at compile-time so I
want to take advantage of that.

Thanks,
Chris
 
Reply With Quote
 
 
 
 
88888 Dihedral
Guest
Posts: n/a
 
      02-08-2012
在 2012年2月8日星期三UTC+8上午8时59分41秒 Chris McAce写道:
> Hi,
>
> I've written a function template for applying a free function to every
> element of a std::vector:
>
> #include <vector>
>
> template<class T, T F(T)>
> std::vector<T> map(const std::vector<T>& from)
> {
> std::vector<T> to;
> to.reserve(from.size());
> for (typename std::vector<T>::const_iterator iter = from.begin();
> iter != from.end(); ++iter) {
> to.push_back(F(*iter));
> }
> return to;
> }
>
> This can be used as follows:
>
> #include <cmath>
>
> int main()
> {
> std::vector<float> angles;
> angles.push_back(1);
> angles.push_back(2);
> angles.push_back(3);
> std::vector<float> sines = map<float, std::sin>(angles);
> }
>
> As you can see, std::sin is passed as a non-type template parameter.
> However, I'd like to get rid of the the first parameter (float in the
> example) so the call would become map<std::sin>(angles). Is there a
> way to do this in C++03 or C++11?
>
> Please note that I don't want to pass function pointers at run-time;
> the mapping function (here, std::sin) is known at compile-time so I
> want to take advantage of that.
>
> Thanks,
> Chris


A template function that accepts any data type and any known function to
operate on a vector of objects to be compiled is different from
an unknown function pointer passed in by the caller part in the run time.

Any object in a vector can be mapped as in the above code.
The constructor has to be called for each new object pushed into the result
vector to.

Note if the object is very large and the length of the vector is large, too
then the above code might run into problems in the heap space.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
Deduce function template argument Ed C++ 1 08-14-2008 08:13 PM
template function argument deduce George2 C++ 0 03-11-2008 12:39 PM
"could not deduce template argument" error gretean@comcast.net C++ 7 11-10-2006 03:03 PM
Forcing all function template parameters to deduce the same type Dan Krantz C++ 3 06-13-2006 02:28 PM



Advertisments