473,396 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

std::generate() functor argument with member functions

Hi,

given a class

class foo
{
private:
void A();
type B();
};

I want to call in A()
std::generate(V.begin(), V.end(), B);

where V is just a standard container of elements of the type "type", but it
does NOT necessarly contains instances of the class foo.

The idea is that B is a member function of foo but that the objects in the
container are not of the type foo.
I cannot make B static 'cause it uses members variables of the class foo.

I supposed I need something like std::generator(V.begin(), V.end(),
bind_func(* this, B)) but what exactly?

Best regards,

Tanguy
Jul 22 '05 #1
3 3935
"Tanguy Fautré" <tf*****@pandora.be> wrote in message
news:T8*********************@phobos.telenet-ops.be...
Hi,

given a class

class foo
{
private:
void A();
type B();
};

I want to call in A()
std::generate(V.begin(), V.end(), B);

where V is just a standard container of elements of the type "type", but it does NOT necessarly contains instances of the class foo.

The idea is that B is a member function of foo but that the objects in the
container are not of the type foo.
I cannot make B static 'cause it uses members variables of the class foo.

I supposed I need something like std::generator(V.begin(), V.end(),
bind_func(* this, B)) but what exactly?


The third argument can be an object that has a () operator (i.e., function
call operator). Being an object, you can create it with whatever members you
like, which enables you to access anything without the need for any static
function or data. e.g.,

class foo;

class foo_B
{
foo &f;
public:
foo_B(foo &f_) : f(f_) {}
type operator()() { return f.B(); }
};

class foo
{
// data members
private:
friend class foo_B;
void A()
{ std::generate(V.begin(), V.end(), foo_B(*this));
}
type B();
};

DW

Jul 22 '05 #2
"David White" <no@email.provided> wrote in message
news:5S******************@nasal.pacific.net.au...
class foo;

class foo_B
{
foo &f;
public:
foo_B(foo &f_) : f(f_) {}
type operator()() { return f.B(); }
Sorry, this won't compile. Make it:
type operator()();
};

class foo
{
// data members
private:
friend class foo_B;
void A()
{ std::generate(V.begin(), V.end(), foo_B(*this));
}
type B();
};


type foo_B::operator()() { return f.B(); }

DW

Jul 22 '05 #3

"David White" <no@email.provided> wrote in message
news:f3******************@nasal.pacific.net.au...
"David White" <no@email.provided> wrote in message
news:5S******************@nasal.pacific.net.au...
class foo;

class foo_B
{
foo &f;
public:
foo_B(foo &f_) : f(f_) {}
type operator()() { return f.B(); }


Sorry, this won't compile. Make it:
type operator()();
};

class foo
{
// data members
private:
friend class foo_B;
void A()
{ std::generate(V.begin(), V.end(), foo_B(*this));
}
type B();
};


type foo_B::operator()() { return f.B(); }

DW

Found what I needed in the Boost Lib :-)

std::generate(V.begin(), V.end(), boost::bind(&foo::B, this));
Yours,

Tanguy
Jul 22 '05 #4

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

Similar topics

8
by: Manfred | last post by:
Hello I am new to template programming, so i tried the 'example' from http://www.sgi.com/tech/stl/functors.html. I can compile the code but when i want to run the program I get a segmentation...
18
by: ma740988 | last post by:
Trying to get more acclimated with the use of function objects. As part of my test, consider: # include <vector> # include <iostream> # include <algorithm> #include <stdexcept> #include...
5
by: Chameleon | last post by:
I totally messed up with this: We have -------------------------------------- generate(v.begin(), v.end(), my_func); -------------------------------------- and my_func: ----------- int...
8
by: rupert | last post by:
It seems a need a paradime shift in understanding how C++ use libraries. Sun keeps is together under their API. And Javadoc enables a neat way of displaying functions of a local program to display...
3
by: Jon Rea | last post by:
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,...
2
by: Lionel B | last post by:
I have a function which takes a functor argument. I which to call it for a functor which is actually a class member; this works fine, using the mem_fun_ref and bind1st functions (see listing 1...
2
by: psujkov | last post by:
Hi everybody, does anyone knows is there any way to have an implicit iteration counter in std::for_each ? e.g. std::for_each(c.begin(), c.end(), boost::bind(&C::set_id, _1,...
3
by: Bill Woessner | last post by:
I'm trying to replace a loop with a call to std::generate and I'm about at wits end. Here's the working code: std::vector<double>::iterator it; std::vector<doubledata(100);...
58
by: Mark Casternoff | last post by:
I'm getting back into C++ after a long hiatus (they didn't have namespaces back then). I know this question is completely subjective, but I'd be interested in hearing which is the "better"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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,...

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.