Solved

Use of generic function with std::transform

Posted on 2013-12-18
3
353 Views
Last Modified: 2013-12-24
Suppose I write a generic function to calculate the square of a number

template<class T> T square(const T & in)
{
	return in*in;
}

Open in new window

and I have a vector of integers called v. When I try to run the following code, I get an error:

std::vector<int> v2;
std::transform(v.begin(), v.end(), std::back_inserter(v2), square);

Open in new window

I get an error. However, if I create the following struct

struct squareTempl
{
	template< typename T >
	T operator ()(const T& in) const {return in*in;}
};

Open in new window

then I can use

std::transform(v.begin(), v.end(), std::back_inserter(v2), squareTempl());

Open in new window

without any problems. Why is there such a discrepancy?
0
Comment
Question by:Rothbard
3 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 125 total points
ID: 39727499
That's not really a discrepancy, 'transform()' (http://www.sgi.com/tech/stl/transform.html) requires a class/struct modeled after 'UnaryFunction'  (http://www.sgi.com/tech/stl/UnaryFunction.html) for this transformation. And a 'UnaryFunction' function object (http://www.sgi.com/tech/stl/functors.html) needs to provide an overloaded 'operator ()'.
0
 
LVL 40

Accepted Solution

by:
evilrix earned 125 total points
ID: 39727695
In the case of the first example you are trying to pass a template function declaration rather than a template function instance. This isn't allowed. The 2nd version works because you are passing an instantiated object. You can make the first version work by instantiating the template.

std::vector<int> v2;
std::transform(v.begin(), v.end(), std::back_inserter(v2), square<int>);

Notice that I am explicitly instantiating the template function for type int.

Put another way "square" is not a concrete function but "square<int>" is.
squareTempl *is* a concrete object.
0
 

Author Comment

by:Rothbard
ID: 39738440
Thanks! Sorry for the delay in acknowledging your responses.

Merry Christmas :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now