Solved

Use of generic function with std::transform

Posted on 2013-12-18
3
352 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

744 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

11 Experts available now in Live!

Get 1:1 Help Now