remove_if and functions overloading

M

marco_segurini

Hi,

if I compile the following code I get the error:

RemoveIf.cpp(19) : error C2914: 'remove_if' : cannot deduce template
argument as function argument is ambiguous

#include <algorithm>
#include <vector>

template<typename T>
class vector_t
: public std::vector<T>
{
bool TestRemove(long Pos) const //(1)
{
return false;
}
static bool TestRemove(const T &) //(2)
{
return false;
}
public:
void RemoveIf()
{
erase(std::remove_if(begin(), end(), &vector_t<T>::TestRemove),
end()); //this is line 19
}
};

typedef struct point
{
long x;
long y;
long f;
} point;

int main()
{
vector_t<point> vp;
vp.RemoveIf();

return 0;
}


How may I force the remove_if to use the TestRemove (2) function as
predicate?

TIA.
Marco.
 
T

tom_usenet

Hi,

if I compile the following code I get the error:

RemoveIf.cpp(19) : error C2914: 'remove_if' : cannot deduce template
argument as function argument is ambiguous

#include <algorithm>
#include <vector>

template<typename T>
class vector_t
: public std::vector<T>
{
bool TestRemove(long Pos) const //(1)
{
return false;
}
static bool TestRemove(const T &) //(2)
{
return false;
}
public:
void RemoveIf()
{

bool (*remover)(const T&) = &vector_t<T>::TestRemove;


erase(std::remove_if(begin(), end(), remover),
end()); //this is line 19
How may I force the remove_if to use the TestRemove (2) function as
predicate?

Either the above, or add a static cast to select the function type:

erase(
std::remove_if(
begin(),
end(),
static_cast<bool (*)(const T&)>(&vector_t<T>::TestRemove)
),
end()
);

When taking the address of an overloaded function, you need a target
type for the compiler to be able to work out which overload you are
referring to. You can specify the target type using an assignment or a
static_cast.

Tom
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top