Solved

Use of generic function with std::transform

Posted on 2013-12-18
3
370 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 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.

730 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