Compilation errors calling find_if using a functor
We are having a bit of trouble using find_if to search a vector of pairs for an entry in which the first element of the pair matches a particular value. To make this work, we have defined a trivial functor whose operator() takes a pair as input and compares the first entry against a string.
Unfortunately, when we actually add a call to find_if using an instance of our functor constructed using a temporary string val开发者_JAVA技巧ue, the compiler produces a raft of error messages. Oddly (to me, anyway), if we replace the temporary with a string that we've created on the stack, things seem to work.
Here's what the code (including both versions) looks like:
typedef std::pair<std::string, std::string> MyPair;
typedef std::vector<MyPair> MyVector;
struct MyFunctor: std::unary_function <const MyPair&, bool>
{
explicit MyFunctor(const std::string& val)
: m_val(val) {}
bool operator() (const MyPair& p)
{
return p.first == m_val;
}
const std::string m_val;
};
bool f(const char* s)
{
MyFunctor f(std::string(s)); // ERROR
// std::string str(s);
// MyFunctor f(str); // OK
MyVector vec;
MyVector::const_iterator i = std::find_if(vec.begin(), vec.end(), f);
return i != vec.end();
}
And here's what the most interesting error message looks like:
/usr/include/c++/4.2.1/bits/stl_algo.h:260: error: conversion from ‘std::pair, std::allocator >, std::basic_string, std::allocator > >’ to non-scalar type ‘std::string’ requested
Because we have a workaround, we're mostly curious as to why the first form causes problems. I'm sure we're missing something, but we haven't been able to figure out what it is.
This is the most vexing parse.
You can do:
MyFunctor f(s);
or
MyFunctor f((std::string(s)));
The original declares a function f
. f
takes a single argument, a pointer to a function taking an argument s
and returning std::string
.
MyFunctor f = MyFunctor(s);
is more clear and works the same.
The first error at
MyFunctor f(std::string(s));
is because std::string(s) is a rvalue(temporary) and the function
explicit MyFunctor(const std::string& val)
requires a reference that cannot be taken from rvalue.
The second error message you got is because you iterating over vector<pair<string, string>>
which requires a functor that takes input of type pair<string, string>
but your functor accepts input of type string
which leads to the compilation error.
精彩评论