Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #13833 > unrolled thread

Deduce non-type template parameter return value

Started byChris McAce <mcace@chello.at>
First post2012-02-07 16:59 -0800
Last post2012-02-08 00:18 -0800
Articles 2 — 2 participants

Back to article view | Back to comp.lang.c++


Contents

  Deduce non-type template parameter return value Chris McAce <mcace@chello.at> - 2012-02-07 16:59 -0800
    Re: Deduce non-type template parameter return value 88888 Dihedral <dihedral88888@googlemail.com> - 2012-02-08 00:18 -0800

#13833 — Deduce non-type template parameter return value

FromChris McAce <mcace@chello.at>
Date2012-02-07 16:59 -0800
SubjectDeduce non-type template parameter return value
Message-ID<8eb47473-94be-41e6-8c3d-af4a88dbf3cc@j14g2000vba.googlegroups.com>
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

[toc] | [next] | [standalone]


#13840

From88888 Dihedral <dihedral88888@googlemail.com>
Date2012-02-08 00:18 -0800
Message-ID<29415009.344.1328689117791.JavaMail.geo-discussion-forums@prfs3>
In reply to#13833
在 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. 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web