can not reference make_pair?

J

John Black

Hi,
I have this code piece,

vector<int> v1, v2;
vector<pair<int, int> > uups;
....

transform(v1.begin(), v1.end(), v2.begin(), uups.end(),
ptr_fun(make_pair));

But my gcc compiler complains that "no matching function for call to
`ptr_fun(<unknown type>". Do you know what's wrong here?

Thanks.
 
J

Jonathan Turkanis

John Black said:
Hi,
I have this code piece,

vector<int> v1, v2;
vector<pair<int, int> > uups;
....

transform(v1.begin(), v1.end(), v2.begin(), uups.end(),
ptr_fun(make_pair));

std::make_pair is a function template; to retrieve a function pointer to one of
its specializations you must use a cast or assign it to a function pointer
variable, as in the following:

#include <algorithm>
#include <functional>
#include <utility>
#include <vector>

using namespace std;

int main()
{
pair<int, int> (*make_int_pair) (int, int) = make_pair;

vector<int> v1, v2;
vector<pair<int, int> > uups;
transform(v1.begin(), v1.end(), v2.begin(), uups.end(),
ptr_fun(make_int_pair));
}

One more thing: I'm not sure the standard guarantees that make_pair doesn't have
additional function parameters with default values. Probably someone else here
knows.

Jonathan
 
E

ES Kim

John Black said:
Hi,
I have this code piece,

vector<int> v1, v2;
vector<pair<int, int> > uups;
....

transform(v1.begin(), v1.end(), v2.begin(), uups.end(),
ptr_fun(make_pair));

But my gcc compiler complains that "no matching function for call to
`ptr_fun(<unknown type>". Do you know what's wrong here?

Thanks.

make_pair is not a function, just a template. But make_pair<int, int> is.
One more thing: you must "push_back" the result into uups, not just
placing it at the end.

transform(v1.begin(), v1.end(), v2.begin(), back_inserter(uups),
ptr_fun(make_pair<int, int>));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top