Solved

Use of generic function with std::transform

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

856 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