Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Use of generic function with std::transform

Posted on 2013-12-18
3
Medium Priority
?
399 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 500 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

688 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