I have the following simple test case: #include <boost/bind.hpp> #include <boost/function.hpp> #include <iostream> using namespace boost; using namespace std; void show(int x) { cout << x << endl; } void twice(function0<void> f) { f(); f(); } int main() { // This works... function<void()> f = bind(show, 0); bind(twice, f)(); // ...but this does not compile. // bind(twice, bind(show, 0))(); return 0; } When uncommenting the problematic line and attempting to build, I get: $ g++ -Wall -o boost_function{,.cc} /opt/armed/include/boost/bind.hpp: In member function ‘void boost::_bi::list1<A1>::operator()(boost::_bi::type<void>, F&, A&, int) [with F = void (*)(boost::function0<void, std::allocator<boost::function_base> >), A = boost::_bi::list0, A1 = boost::_bi::bind_t<void, void (*)(int), boost::_bi::list1<boost::_bi::value<int> > >]’: /opt/armed/include/boost/bind/bind_template.hpp:20: instantiated from ‘typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()() [with R = void, F = void (*)(boost::function0<void, std::allocator<boost::function_base> >), L = boost::_bi::list1<boost::_bi::bind_t<void, void (*)(int), boost::_bi::list1<boost::_bi::value<int> > > >]’ boost_function.cc:13: instantiated from here /opt/armed/include/boost/bind.hpp:232: error: conversion from ‘void’ to non-scalar type ‘boost::function0<void, std::allocator<boost::function_base> >’ requested Why can't I put the two on the same line? (I also tried using template<typename T> void twice(T f) { f(); f() }, but that didn't work either.) Thanks in advance for any tips. -- Yang Zhang http://www.mit.edu/~y_z/
I have the following simple test case: void show(int x) { cout << x << endl; } void twice(function0<void> f) { f(); f(); } int main() { // This works... function<void()> f = bind(show, 0); bind(twice, f)(); // ...but this does not compile. // bind(twice, bind(show, 0))(); return 0; }
You can do a cast: boost::bind(twice, (boost::function<void()>)boost::bind(show, 0))(); I'm guessing that as the function just returns a void, boost::bind binds twice() to the returned void rather than a function ptr. Hopefully there is a more elegant solution that the above... ****************************************************************************** "This message and any attachments are solely for the intended recipient and may contain confidential and privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you." Interactive Transaction Solutions Ltd (2473364 England) Registered Office: Systems House, Station Approach Emsworth PO10 7PW ********************************************************************** Ce message �lectronique contient des informations confidentielles � l'usage unique des destinataires indiqu�s, personnes physiques ou morales. Si vous n'�tes pas le destinataire voulu, toute divulgation, copie, ou diffusion ou toute autre utilisation de ces informations, est interdite. Si vous avez re�u ce message �lectronique par erreur, nous vous remercions d'en avertir son exp�diteur imm�diatement par email et de d�truire ce message ainsi que les �l�ments attach�s. Interactive transaction Solutions SAS- France (RCS Pontoise : 489 397 877) Si�ge social : Parc Saint Christophe, 10, Avenue de l�Entreprise 95865 Cergy-Pontoise Cedex ______________________________________________________________________ This email has been scanned by the MessageLabs Email Security System. For more information please visit http://www.messagelabs.com/email ______________________________________________________________________
Steven Watanabe wrote:
AMDG
Yang Zhang wrote:
// ...but this does not compile. // bind(twice, bind(show, 0))();
#include <boost/bind/protect.hpp>
...
bind(twice, protect(bind(show, 0)))
Thanks, this works. Would anybody be able to explain what just happened? According to: http://www.boost.org/doc/libs/1_37_0/libs/bind/bind.html#nested_binds this is supposed to prevent something from being evaluated, but I can't tell what precisely was going on. I believe my question of understanding also ties into why their `apply` example was necessary.
In Christ, Steven Watanabe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Yang Zhang http://www.mit.edu/~y_z/
AMDG Yang Zhang wrote:
bind(twice, protect(bind(show, 0)))
Thanks, this works. Would anybody be able to explain what just happened? According to:
bind(twice, bind(show, 0))() is evaluated as twice(show(0)) You want: twice(bind(show, 0)) The protect prevents show from being evaluated immediately by Boost.Bind. In Christ, Steven Watanabe
On Thu, Nov 20, 2008 at 08:28:35AM -0800, Steven Watanabe wrote:
The protect prevents show from being evaluated immediately by Boost.Bind.
In other words, does protect allow "lazy" evaluation? Matthias -- Matthias Vallentin vallentin@icsi.berkeley.edu http://matthias.vallentin.cc
participants (4)
-
Matthias Vallentin
-
Patrick Loney
-
Steven Watanabe
-
Yang Zhang