How to use bind library to de-reference a pointer

I have a list of A pointers: vector <A*> aVector; And i have a function which takes a A reference. void myFunc(A& a); how can I loop thru aVector to call myFunct? for_each (aVector;.begin(), aVector;.end(), bind (&myFunc, _1)); // does not compile. Thanks for any help.

On 2/17/06, Sebastian Redl <sebastian.redl@getdesigned.at> wrote:
How about an indirect_iterator?
Or maybe a ptr_vector, so that your iterators are automatically indirect? lambda needs result deduction, which is hard, but you could always write your own simple dereferencing functor along the lines of the following: template <typename T> struct dereference_as { template <typename P> T &operator()(P ptr) { return *ptr; } } and then for_each( aVector.begin(), aVector.end(), bind(&myFunc, bind( dereference_as<A>(), _1) ) ); ~ Scott McMurray

Thanks. but when I do that, I get a compile error saying PageBreaker.cpp:189: instantiated from here A.h:268: error: 'A::A(const A&)' is private Why boost is trying to make a copy of A (a vector is a list of A*). And how can I fix that without making A constructor public? Thank you. On 17 Feb 2006 11:23:00 +0000, Jens Theisen <jth01@arcor.de> wrote:
Meryl wrote:
for_each (aVector;.begin(), aVector;.end(), bind (&myFunc, _1)); //
for_each( aVector.begin(), aVector.end(), bind(&myFunc, *_1) );
But that's boost::lambda rather than boost::bind.
Jens
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On 2/17/06, Meryl Silverburgh <silverburgh.meryl@gmail.com> wrote:
Why boost is trying to make a copy of A (a vector is a list of A*).
And how can I fix that without making A constructor public?
I have no idea why it'd be doing that -- some more code would be helpful. The following testcase seems to work fine: #include <iostream> #include <vector> #include <algorithm> #include <boost/bind.hpp> template <typename T> struct dereference_as { typedef T& result_type; template <typename P> result_type operator()(P ptr) { return *ptr; } }; void showfirst(char c) { std::cout << c << std::endl; } int main() { std::vector<char const*> v; v.push_back( "1" ); v.push_back( "2" ); v.push_back( "3" ); v.push_back( "4" ); v.push_back( "5" ); v.push_back( "6" ); std::for_each( v.begin(), v.end(), boost::bind( &showfirst, boost::bind( dereference_as<char const>(), _1 ) ) ); } ~ Scott

My case is using a vector of a pointer of a Class say 'A' which that class's constructor is private. On 2/17/06, me22 <me22.ca@gmail.com> wrote:
On 2/17/06, Meryl Silverburgh <silverburgh.meryl@gmail.com> wrote:
Why boost is trying to make a copy of A (a vector is a list of A*).
And how can I fix that without making A constructor public?
I have no idea why it'd be doing that -- some more code would be helpful.
The following testcase seems to work fine: #include <iostream> #include <vector> #include <algorithm> #include <boost/bind.hpp>
template <typename T> struct dereference_as { typedef T& result_type; template <typename P> result_type operator()(P ptr) { return *ptr; } };
void showfirst(char c) { std::cout << c << std::endl; }
int main() {
std::vector<char const*> v; v.push_back( "1" ); v.push_back( "2" ); v.push_back( "3" ); v.push_back( "4" ); v.push_back( "5" ); v.push_back( "6" );
std::for_each( v.begin(), v.end(), boost::bind( &showfirst, boost::bind( dereference_as<char const>(), _1 ) ) ); }
~ Scott
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Meryl wrote:
My case is using a vector of a pointer of a Class say 'A' which that class's constructor is private.
The following is working for me: #include <iostream> #include <vector> #include <algorithm> #include <boost/bind.hpp> #include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> namespace lambda = boost::lambda; struct A { private: A(); }; void dummy(A const& c) { } int main() { std::vector<A*> v; std::for_each ( v.begin(), v.end(), bind ( &dummy, *lambda::_1 ) ); } Do you have a test case? Jens
participants (4)
-
jth01@arcor.de
-
me22
-
Meryl Silverburgh
-
Sebastian Redl