Google Groups Home
Help | Sign in
adapter function for for_each
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
dominolog  
View profile
(1 user)  More options Aug 20, 9:55 am
Newsgroups: comp.lang.c++
From: dominolog <dominiktomc...@gmail.com>
Date: Wed, 20 Aug 2008 01:55:10 -0700 (PDT)
Local: Wed, Aug 20 2008 9:55 am
Subject: adapter function for for_each
Hello

I've got following container:

std::vector<boost::shared_ptr<std::string> > strings;
strings.push_back( boost::shared_ptr<std::string> ( new
std::string("AAA") ) );
strings.push_back( boost::shared_ptr<std::string> ( new
std::string("BBB") ) );
strings.push_back( boost::shared_ptr<std::string> ( new
std::string("BBB") ) );

std::for_each( strings.begin(), strings.end(),
std::mem_fun( &std::length ) );

Now I want a 0-parameter method from std::string to be called on every
element from the sequence. I tried to use std::mem_fun (as above), but
it claims as follows:

>> cannot convert parameter 1 from 'boost::shared_ptr<T>' to 'const std::basic_string<_Elem,_Traits,_Ax> *'

The question is - how to construct a correct adapter for it, not using
a custom operand class. I want to use only stl stuff.

Thanks


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Giovanni Gherdovich  
View profile
(1 user)  More options Aug 20, 3:06 pm
Newsgroups: comp.lang.c++
From: Giovanni Gherdovich <gherdov...@students.math.unifi.it>
Date: Wed, 20 Aug 2008 07:06:41 -0700 (PDT)
Local: Wed, Aug 20 2008 3:06 pm
Subject: Re: adapter function for for_each
Hello,

I don't know how you can solve the problem,
but at a first glance I'm afraid that a
smart pointer (boost::shared_ptr in your case)
cannot be used where a "real" pointer is needed
(but I'm not sure).

The compiler tells that he expects an object of
type 'const std::string *' (std::string is just an alias
for std::basic_string<char>), he found an object
of type boost::shared_ptr<string>.

I feel that a smart pointer acts _logically_ as a pointer,
but _is not_ a pointer...

Maybe you've already tried, but I remark that your
code compiles fine if you substitute
boost::shared_ptr<std::string> with string*, i.e.

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

int main() {
std::vector<std::string* > strings;
strings.push_back( (new std::string("AAA") ) );
strings.push_back( (new std::string("BBB") ) );
strings.push_back( (new std::string("BBB") ) );

std::for_each( strings.begin(), strings.end(),
std::mem_fun( &std::string::length ) );


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mlimber  
View profile
(1 user)  More options Aug 20, 4:08 pm
Newsgroups: comp.lang.c++
From: mlimber <mlim...@gmail.com>
Date: Wed, 20 Aug 2008 08:08:46 -0700 (PDT)
Local: Wed, Aug 20 2008 4:08 pm
Subject: Re: adapter function for for_each
On Aug 20, 4:55 am, dominolog <dominiktomc...@gmail.com> wrote:

Since you're using Boost (or TR1), you can use boost::mem_fn (or
std::tr1::mem_fn) or boost::bind (or std::tr1::bind):

 std::for_each( strings.begin(), strings.end(),
    boost::mem_fn(&std::string::length));

Or

 std::for_each( strings.begin(), strings.end(),
    std::tr1::mem_fn(&std::string::length));

Or

 std::for_each( strings.begin(), strings.end(),
    boost::bind(&std::string::length, _1));

Or

 std::for_each( strings.begin(), strings.end(),
    std::tr1::bind(&std::string::length, std::tr1::placeholders::_1));

Cheers! --M


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mlimber  
View profile
(1 user)  More options Aug 20, 4:20 pm
Newsgroups: comp.lang.c++
From: mlimber <mlim...@gmail.com>
Date: Wed, 20 Aug 2008 08:20:07 -0700 (PDT)
Local: Wed, Aug 20 2008 4:20 pm
Subject: Re: adapter function for for_each
On Aug 20, 10:06 am, Giovanni Gherdovich
<gherdov...@students.math.unifi.it> wrote:

[snip]
> Maybe you've already tried, but I remark that your
> code compiles fine if you substitute
> boost::shared_ptr<std::string> with string*, i.e.

[snip]

Sure, but one of the great things about shared_ptr (and some other
smart pointers) is that it allows easy use of pointers in STL
containers. Switching to raw pointers eliminates automatic cleanup
(especially important in the face of exceptions), shared ownership,
etc., which the OP may need. Using RAII techniques, like that
implemented by shared_ptr, is one of the best ways to eliminate memory
(and other resource) leaks. See Sutter and Alexandrescu, _C++ Coding
Standards_, Item 13.

Cheers! --M

Cheers! --M


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
dominolog  
View profile
 More options Aug 20, 5:19 pm
Newsgroups: comp.lang.c++
From: dominolog <dominiktomc...@gmail.com>
Date: Wed, 20 Aug 2008 09:19:52 -0700 (PDT)
Local: Wed, Aug 20 2008 5:19 pm
Subject: Re: adapter function for for_each
On 20 Sie, 17:20, mlimber <mlim...@gmail.com> wrote:

Thanks

I haven't considered boost::mem_fun_ref. It works. In terms of
string*, it was an example, I actually use more havy classes in my
containers but for simplicity provided an example with
shared_ptr<string>.

Anyway thanks.

dominolog


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Giovanni Gherdovich  
View profile
 More options Aug 21, 10:29 am
Newsgroups: comp.lang.c++
From: Giovanni Gherdovich <gherdov...@students.math.unifi.it>
Date: Thu, 21 Aug 2008 02:29:27 -0700 (PDT)
Local: Thurs, Aug 21 2008 10:29 am
Subject: Re: adapter function for for_each
Hello,

On Aug 20, 5:20 pm, mlimber <mlim...@gmail.com> wrote:

> Sure, but one of the great things about shared_ptr (and some other
> smart pointers) is that it allows easy use of pointers in STL
> containers. Switching to raw pointers eliminates automatic cleanup
> (especially important in the face of exceptions), shared ownership,
> etc., which the OP may need. Using RAII techniques, like that
> implemented by shared_ptr, is one of the best ways to eliminate memory
> (and other resource) leaks. See Sutter and Alexandrescu, _C++ Coding
> Standards_, Item 13.

Thank you for the reference to Sutter & Alexandrescu!

Giovanni


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google