[bind] How to dereference _1 in a boost::bind expression

Hello, Here's an example of my problem: #include <algorithm> #include <vector> #include <boost/bind.hpp> class TheClass { }; void accept(TheClass& tc) { } int main() { std::vector<TheClass*> cont; // Compile error - how to dereference _1 ??? std::for_each(cont.begin(),cont.end(),boost::bind(&accept,_1)); } How can I dereference _1 so I can use the accept function? Regards, Pete

On Thu, Oct 2, 2008 at 11:12 AM, Peter Barker <newbarker@gmail.com> wrote:
Hello,
Here's an example of my problem:
#include <algorithm> #include <vector>
#include <boost/bind.hpp>
class TheClass { };
void accept(TheClass& tc) { }
int main() { std::vector<TheClass*> cont;
// Compile error - how to dereference _1 ??? std::for_each(cont.begin(),cont.end(),boost::bind(&accept,_1)); }
How can I dereference _1 so I can use the accept function?
#include <algorithm> #include <vector> #include <boost/lambda/bind.hpp> #include <boost/lambda/lambda.hpp> class TheClass { }; void accept(TheClass& tc) { } int main() { using namespace boost; using namespace boost::lambda; std::vector<TheClass*> cont; // Compile error - how to dereference _1 ??? std::for_each(cont.begin(),cont.end(),bind(accept,*_1)); } - Rob.

Robert Jones schrieb:
On Thu, Oct 2, 2008 at 11:12 AM, Peter Barker <newbarker@gmail.com> wrote:
Hello,
Here's an example of my problem:
#include <algorithm> #include <vector>
#include <boost/bind.hpp>
class TheClass { };
void accept(TheClass& tc) { }
int main() { std::vector<TheClass*> cont;
// Compile error - how to dereference _1 ??? std::for_each(cont.begin(),cont.end(),boost::bind(&accept,_1)); }
How can I dereference _1 so I can use the accept function?
#include <algorithm> #include <vector>
#include <boost/lambda/bind.hpp> #include <boost/lambda/lambda.hpp>
class TheClass { };
void accept(TheClass& tc) { }
int main() { using namespace boost; using namespace boost::lambda;
std::vector<TheClass*> cont;
// Compile error - how to dereference _1 ??? std::for_each(cont.begin(),cont.end(),bind(accept,*_1)); }
- Rob. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hi Rob, with std::for_each(cont.begin(),cont.end(),bind(&accept,*_1)); you can dereference _1. Best regards Franz

On Thu, Oct 2, 2008 at 11:51 AM, Franz Alt <f.alt@gmx.net> wrote:
Robert Jones schrieb:
On Thu, Oct 2, 2008 at 11:12 AM, Peter Barker <newbarker@gmail.com> wrote:
Hello,
Here's an example of my problem:
#include <algorithm> #include <vector>
#include <boost/bind.hpp>
class TheClass { };
void accept(TheClass& tc) { }
int main() { std::vector<TheClass*> cont;
// Compile error - how to dereference _1 ??? std::for_each(cont.begin(),cont.end(),boost::bind(&accept,_1)); }
How can I dereference _1 so I can use the accept function?
#include <algorithm> #include <vector>
#include <boost/lambda/bind.hpp> #include <boost/lambda/lambda.hpp>
class TheClass { };
void accept(TheClass& tc) { }
int main() { using namespace boost; using namespace boost::lambda;
std::vector<TheClass*> cont;
// Compile error - how to dereference _1 ??? std::for_each(cont.begin(),cont.end(),bind(accept,*_1)); }
- Rob. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hi Rob,
with
std::for_each(cont.begin(),cont.end(),bind(&accept,*_1));
you can dereference _1.
Best regards
Franz
Franz, I think that gave an illegal indirection compiler error. Regards, Pete

On Thu, Oct 2, 2008 at 11:39 AM, Robert Jones <robertgbjones@gmail.com> wrote:
On Thu, Oct 2, 2008 at 11:12 AM, Peter Barker <newbarker@gmail.com> wrote:
Hello,
Here's an example of my problem:
#include <algorithm> #include <vector>
#include <boost/bind.hpp>
class TheClass { };
void accept(TheClass& tc) { }
int main() { std::vector<TheClass*> cont;
// Compile error - how to dereference _1 ??? std::for_each(cont.begin(),cont.end(),boost::bind(&accept,_1)); }
How can I dereference _1 so I can use the accept function?
#include <algorithm> #include <vector>
#include <boost/lambda/bind.hpp> #include <boost/lambda/lambda.hpp>
class TheClass { };
void accept(TheClass& tc) { }
int main() { using namespace boost; using namespace boost::lambda;
std::vector<TheClass*> cont;
// Compile error - how to dereference _1 ??? std::for_each(cont.begin(),cont.end(),bind(accept,*_1)); }
Rob, Thanks for the suggestion - my sample program was actually too stripped down. TheClass is a base with pure virtual methods and your suggestion gives me the "cannot instantiate abstract class" compiler error. Here's a new sample program: #include <algorithm> #include <vector> #include <boost/lambda/bind.hpp> #include <boost/lambda/lambda.hpp> class TheClass { public: virtual ~TheClass() {} virtual void pvm() = 0; }; class DerivedClass : public TheClass { void pvm() {} }; void accept(TheClass& tc) { } int main() { std::vector<TheClass*> cont; using namespace boost::lambda; using boost::lambda::_1; // Compile error - cannot instantiate abstract class - how to dereference _1 ??? std::for_each(cont.begin(),cont.end(),bind(&accept,*_1)); }

I think this may be a real limitation. See, http://lists.boost.org/Archives/boost/2008/07/139932.php It's not quite what you're trying to do, but close. Perhaps someone more knowledgeable than I will show us the way! - Rob.

On Thu, 2 Oct 2008 11:12:54 +0100 "Peter Barker" <newbarker@gmail.com> wrote:
Hello,
Here's an example of my problem:
#include <algorithm> #include <vector>
#include <boost/bind.hpp>
class TheClass { };
void accept(TheClass& tc) { }
int main() { std::vector<TheClass*> cont;
// Compile error - how to dereference _1 ??? std::for_each(cont.begin(),cont.end(),boost::bind(&accept,_1)); }
How can I dereference _1 so I can use the accept function?
Regards,
Pete _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
You don't have to dereference _1. Your accept function must look like this: void accept(TheClass *arg) { } Your std::vector contains pointers to TheClass so std::for_each() passes objects of that type to your functor, in this case a pointer to types of TheClass. Sven

On Thu, Oct 2, 2008 at 11:45 AM, Sven Gaerner <sgaerner@gmx.net> wrote:
On Thu, 2 Oct 2008 11:12:54 +0100 "Peter Barker" <newbarker@gmail.com> wrote:
Hello,
Here's an example of my problem:
#include <algorithm> #include <vector>
#include <boost/bind.hpp>
class TheClass { };
void accept(TheClass& tc) { }
int main() { std::vector<TheClass*> cont;
// Compile error - how to dereference _1 ??? std::for_each(cont.begin(),cont.end(),boost::bind(&accept,_1)); }
How can I dereference _1 so I can use the accept function?
Regards,
Pete _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
You don't have to dereference _1. Your accept function must look like this: void accept(TheClass *arg) { } Your std::vector contains pointers to TheClass so std::for_each() passes objects of that type to your functor, in this case a pointer to types of TheClass.
Sven, I was trying to avoid changing the signature of accept as it has other call sites and I would need to update them. It's what I did in the end though. Thanks, Pete

On Thu, 2 Oct 2008 14:36:01 +0100 "Peter Barker" <newbarker@gmail.com> wrote:
On Thu, Oct 2, 2008 at 11:45 AM, Sven Gaerner <sgaerner@gmx.net> wrote:
On Thu, 2 Oct 2008 11:12:54 +0100 "Peter Barker" <newbarker@gmail.com> wrote:
Hello,
Here's an example of my problem:
#include <algorithm> #include <vector>
#include <boost/bind.hpp>
class TheClass { };
void accept(TheClass& tc) { }
int main() { std::vector<TheClass*> cont;
// Compile error - how to dereference _1 ??? std::for_each(cont.begin(),cont.end(),boost::bind(&accept,_1)); }
How can I dereference _1 so I can use the accept function?
Regards,
Pete _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
You don't have to dereference _1. Your accept function must look like this: void accept(TheClass *arg) { } Your std::vector contains pointers to TheClass so std::for_each() passes objects of that type to your functor, in this case a pointer to types of TheClass.
Sven,
I was trying to avoid changing the signature of accept as it has other call sites and I would need to update them. It's what I did in the end though. Thanks,
Pete _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Peter, I didn't think about avoiding to change the signature. Sorry for that. What about using a specific delegate functor? Somethink like that: struct Delegate { void operator()(TheClass *arg) { accept(*arg); } }; And then pass this functor to std::for_each(); std::for_each(cont.begin(),cont.end(), Delegate()); That wouldn't break the signature of the accept function, but it depends on that particular implementation. Sven

Peter Barker: ...
void accept(TheClass& tc) { }
int main() { std::vector<TheClass*> cont;
// Compile error - how to dereference _1 ??? std::for_each(cont.begin(),cont.end(),boost::bind(&accept,_1)); }
How can I dereference _1 so I can use the accept function?
With boost::bind, there's no easy way to dereference _1. You'd need template<class T> T& deref( T* p ) { return *p; } std::for_each( cont.begin(), cont.end(), boost::bind( accept, boost::bind( deref<TheClass>, _1 ) ) );

On Thu, Oct 2, 2008 at 3:25 PM, Peter Dimov <pdimov@pdimov.com> wrote:
Peter Barker: ...
void accept(TheClass& tc) { }
int main() { std::vector<TheClass*> cont;
// Compile error - how to dereference _1 ??? std::for_each(cont.begin(),cont.end(),boost::bind(&accept,_1)); }
How can I dereference _1 so I can use the accept function?
With boost::bind, there's no easy way to dereference _1. You'd need
template<class T> T& deref( T* p ) { return *p; }
std::for_each( cont.begin(), cont.end(), boost::bind( accept, boost::bind( deref<TheClass>, _1 ) ) ); Peter,
It's nice to hear the answer from the horses mouth (so to speak)! Regards, Pete

Peter Barker wrote:
On Thu, Oct 2, 2008 at 3:25 PM, Peter Dimov <pdimov@pdimov.com> wrote:
Peter Barker: ...
void accept(TheClass& tc) { }
int main() { std::vector<TheClass*> cont;
// Compile error - how to dereference _1 ??? std::for_each(cont.begin(),cont.end(),boost::bind(&accept,_1)); }
How can I dereference _1 so I can use the accept function? With boost::bind, there's no easy way to dereference _1. You'd need
template<class T> T& deref( T* p ) { return *p; }
std::for_each( cont.begin(), cont.end(), boost::bind( accept, boost::bind( deref<TheClass>, _1 ) ) ); Peter,
It's nice to hear the answer from the horses mouth (so to speak)!
IIRC, indirect_iterator takes this approach. The following should work as well: std::for_each( make_indirect_iterator(cont.begin()) , make_indirect_iterator(cont.end()) , boost::bind( accept, _1)); Jeff Flinn
participants (6)
-
Franz Alt
-
Jeff Flinn
-
Peter Barker
-
Peter Dimov
-
Robert Jones
-
Sven Gaerner