473,769 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

remove_if and member function pointers

Hello all,

Sorry if this is long, but I wanted to be specific...

Can anyone shed any light on the following errors in the context of the
following example code:

cullMethod1(); // compiles, but not what we want - we cant access
m_Exclustions

cullMethod2(); // fine but long-winded and causes many array re-allocations

cullMethod3(); // doen't compile!

i.e. How do you pass a member function to std functions.

Many thanks,
Jon Rea
////////////////////////////////////////////////////
// Begin example code ..

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

using namespace std;

class Bond
{
public:
Bond( int _i, int _j )
{
i = _i;
j = _j;
}
Bond()
{
i = rand();
j = rand();
}
int i;
int j;
};

class MyBase
{
public:
virtual void Setup()
{
int bigNumber = 10000;
for( int i = 0; i < bigNumber; i++ )
{
m_Bonds.push_ba ck( Bond() ); // add lots of random "bonds"
}
}
protected:
std::vector<Bon dm_Bonds;
};

bool mySpecialSelect ionFunction( Bond& _b )
{
return _b.i == 47;
}

class MyDerived : public MyBase
{
public:
virtual void Setup()
{
MyBase::Setup() ;

m_Exclustions.p ush_back(Bond(4 3,21));
m_Exclustions.p ush_back(Bond(1 2,14));
m_Exclustions.p ush_back(Bond(1 ,41));

// Remove all instances of the definitions in m_Exclustions from the
base class list.

// Call ONE of these in real code:
cullMethod1(); // compiles, but not what we want - we cant access
m_Exclustions
cullMethod2(); // fine but long-whinded and causes many array
re-allocations
cullMethod3(); // doen't compile!
}

bool Matches( Bond& bond )
{
for( size_t k = 0; k < m_Exclustions.s ize(); k++ )
{
if( ( bond.i == m_Exclustions[k].i && bond.j == m_Exclustions[k].j ) ||
( bond.j == m_Exclustions[k].i && bond.i == m_Exclustions[k].j ) )
{
return true;
}
}
return false;
}

void cullMethod1()
{
m_Bonds.erase( remove_if( m_Bonds.begin() , m_Bonds.end(),
mySpecialSelect ionFunction ), m_Bonds.end() );
}

void cullMethod2()
{
for( size_t i = 0; i < m_Bonds.size(); /*blank*/ )
{
if( Matches( m_Bonds[i] ) )
{
m_Bonds.erase(m _Bonds.begin()+ i);
}
else
{
i++;
}
}
}

void cullMethod3()
{
// Illegal operand on bound member function expression ...
//m_Bonds.erase( remove_if( m_Bonds.begin() , m_Bonds.end(), &Matches
), m_Bonds.end() );

// Term does not evaluare to a function taking 1 arguments ...
//m_Bonds.erase( remove_if( m_Bonds.begin() , m_Bonds.end(),
&MyDerived::Mat ches ), m_Bonds.end() );

// Term does not evaluare to a function taking 1 arguments ...
//m_Bonds.erase( remove_if( m_Bonds.begin() , m_Bonds.end(),
std::mem_fun_re f(&MyDerived::M atches) ), m_Bonds.end() );
}

protected:
std::vector<Bon dm_Exclustions;
};

int _tmain(int argc, _TCHAR* argv[])
{
MyDerived bob;
bob.Setup();
return 0;
}
Nov 16 '06 #1
3 4155
In article <45************ **@bris.ac.uk>, Jon Rea <jo*****@bris.a c.uk>
wrote:
Hello all,

Sorry if this is long, but I wanted to be specific...

Can anyone shed any light on the following errors in the context of the
following example code:

cullMethod1(); // compiles, but not what we want - we cant access
m_Exclustions

cullMethod2(); // fine but long-winded and causes many array re-allocations

cullMethod3(); // doen't compile!

i.e. How do you pass a member function to std functions.
First change the signature for Matches:

bool Matches(Bond bond) const
{
...
}

Then you can:

void cullMethod1()
{
m_Bonds.erase(r emove_if(m_Bond s.begin(), m_Bonds.end(),
bind1st(mem_fun (&MyDerived::Ma tches), this)), m_Bonds.end());
}

--
To send me email, put "sheltie" in the subject.
Nov 17 '06 #2
Daniel T. wrote:
In article <45************ **@bris.ac.uk>, Jon Rea <jo*****@bris.a c.uk>
wrote:
>Hello all,

Sorry if this is long, but I wanted to be specific...

Can anyone shed any light on the following errors in the context of the
following example code:

cullMethod1( ); // compiles, but not what we want - we cant access
m_Exclustion s

cullMethod2( ); // fine but long-winded and causes many array re-allocations

cullMethod3( ); // doen't compile!

i.e. How do you pass a member function to std functions.

First change the signature for Matches:

bool Matches(Bond bond) const
{
...
}

Then you can:

void cullMethod1()
{
m_Bonds.erase(r emove_if(m_Bond s.begin(), m_Bonds.end(),
bind1st(mem_fun (&MyDerived::Ma tches), this)), m_Bonds.end());
}
Thanks for the reply. Thats better!

What though if I had to use:
bool Matches(BIG_Bon d& bond) const;
because copying a 'BIG_Bond' is an expensive operation.

Cheers,
Jon
Nov 17 '06 #3
In article <J8********@bat h.ac.uk>, Jon Rea <jo*****@bris.a c.ukwrote:
Daniel T. wrote:
In article <45************ **@bris.ac.uk>, Jon Rea <jo*****@bris.a c.uk>
wrote:
Hello all,

Sorry if this is long, but I wanted to be specific...

Can anyone shed any light on the following errors in the context of the
following example code:

cullMethod1(); // compiles, but not what we want - we cant access
m_Exclustions

cullMethod2(); // fine but long-winded and causes many array re-allocations

cullMethod3(); // doen't compile!

i.e. How do you pass a member function to std functions.
First change the signature for Matches:

bool Matches(Bond bond) const
{
...
}

Then you can:

void cullMethod1()
{
m_Bonds.erase(r emove_if(m_Bond s.begin(), m_Bonds.end(),
bind1st(mem_fun (&MyDerived::Ma tches), this)), m_Bonds.end());
}

Thanks for the reply. Thats better!

What though if I had to use:
bool Matches(BIG_Bon d& bond) const;
because copying a 'BIG_Bond' is an expensive operation.
Test first. I would not be surprised to find that the compiler optimized
the copy away if the Matches function doesn't modify the parameter in
any way.

However, if you absolutely *had* to use a const& the you can always
create your own selector and then use:

m_Bonds.erase(r emove_if(m_Bond s.begin(), m_Bonds.end(),
selector(&MyDer ived::Matches, this)), m_Bonds.end());

Before I show you what "selector" looks like, you may be interested in
how I developed it. I put the above code in as the interface I wished,
and then fixed the compile errors until I had something that ran. After
than, I substituted templated tyeps. QED

template < typename Ret, typename Tp, typename Arg >
struct selector_t : unary_function< Arg, Ret >
{
Ret (Tp::*mfn)(Arg) const;
const Tp* obj;

selector_t(Ret( Tp::*x)(Arg) const, const Tp* y): mfn(x), obj(y) { }

Ret operator()(Arg x) const {
return (obj->*mfn)(x);
}
};

template < typename Ret, typename Tp, typename Arg >
selector_t< Ret, Tp, Arg selector(Ret(Tp ::*x)(Arg) const, Tp* y) {
return selector_t< Ret, Tp, Arg >(x, y);
}

And yes, I double checked. Arg in the above template is a "const Bond&"
and not a "Bond".

--
To send me email, put "sheltie" in the subject.
Nov 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
9377
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1, 2, func);
1
5184
by: Paavo K | last post by:
Hi, can anyone tell what is wrong with following code : //..Code class removeTest { public: bool operator()(map<int, string>::value_type &d) const { // Just returning false to simplify code..
2
2461
by: marco_segurini | last post by:
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>
2
3638
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't support. (first i compiled them with g++3.x. ERR means compiler will bark, otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
37
5021
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well for us.
11
4431
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI components. The application is built around a "World" object that contains a "GUI" object that is a C++ wrapper around GLUT. What I'd like to do is pass a pointer to a member function in the World class to function in the GUI
1
7590
by: pheres | last post by:
Hi, I'm trying to pass pointers to member functions around in my code. The test with pointers to non-member function works fine: Code: void callOut( void (*callback)() ) { callback();
4
3512
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token vector_static_function.c:21: error: expected constructor, destructor, or type conversion before '.' token
3
2592
by: Angus | last post by:
Hi I have a class CRequest with a function: bool IsFinished() const; I have a list of these CRequests - list<CRequestmylist. I create a function object to find if a CRequest is finished: class is_finished : public std::unary_function<CRequest, bool> {
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7409
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3959
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.